一)创建一个简单的Notification,根据谷歌官方介绍的方法,我们利用V4包里的NotificationCompat.Builder的方法来创建最好,也非常简单,主要有几个参数,setSmallIcon()是通知左边的小图标,setContentTitle()是通知的标题,setContentText()是通知的内容,一般标题的字体比内容要大一些。在api11以上也可以使用Notification.Builder来创建.
设置完参数后需要开启NotificationManager,获取它的实例通过
getSystemService(Context.NOTIFICATION_SERVICE)
Builder.build()返回的是一个Notification,Notifictionmanager的notify()方法要传入两个参数,第二个参数就是这个Notification类型。
这样一个通知就创建好了,点击按钮后拉开标题栏,会看到效果
public class MainActivity extends Activity {
private static int id=1200;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
NotificationCompat.Builder builder=new NotificationCompat.Builder(MainActivity.this )
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("啊哦!你有一条新消息")
.setContentText("你已经成功创建一条通知");
NotificationManager mNotificationManager=(NotificationManager) getSystemService
(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());
}
});
}
}
notify()的第一个参数是id,可以用来动态的改变通知的内容,比如我们设置一个count,用来记录点击的次数,每点击一次通知栏的count也会相应的改变
private static int count=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
count++;
NotificationCompat.Builder builder=new NotificationCompat.Builder(MainActivity.this )
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("啊哦!你有"+count+"条新消息")
.setContentText("你已经成功创建一条通知");
NotificationManager mNotificationManager=(NotificationManager) getSystemService
(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on
mNotificationManager.notify(id, builder.build());
}
});
}
比如我点击按钮五次,通知会是这样的:
二)通知显示出来之后,我们需要给其设置点击事件,这里牵涉到PendingIntent的使用。
当从Notification开启一个Activity的时候,一般有两种情况,第一种情况是这个活动新开了一个活动栈,不占用应用的内存,按下返回键会直接返回到Home界面;第二种情况是给PendingIntent添加返回栈,在AndroidManifest文件里指明其父活动,当按下返回键的时候,带有返回栈的PendingIntent会复制原来活动正常的返回行为
1)开返回栈
先给目标活动注册,注意添加父活动,
</intent-filter>
</activity>
<activity
android:name="com.example.notification.ResultActivity"
android:parentActivityName=".MainActivity">
</activity>
如果是android4.0.3之前的还要加上
<meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity"/>然后在点击按钮里,这里在api16以下的要使用v4包里的TaskStackBuilder,我设置的最小sdk是14,所以使用的v4包。
Intent intent=new Intent(MainActivity.this,ResultActivity.class);
android.support.v4.app.TaskStackBuilder stackBuilder=android.support.v4.app.TaskStackBuilder.create(MainActivity.this);
stackBuilder.addParentStack(ResultActivity.class);//添加返回栈
stackBuilder.addNextIntent(intent);//把intent添加到返回栈的栈顶
PendingIntent pd=stackBuilder.getPendingIntent(0, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder=new NotificationCompat.Builder(MainActivity.this )
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("啊哦!你有"+count+"条新消息")
.setContentText("你已经成功创建一条通知")
.setContentIntent(pd);
NotificationManager mNotificationManager=(NotificationManager) getSystemService
(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on
mNotificationManager.notify(id, builder.build());
2)不加返回栈,直接在活动里写PendingIntent就可以
NotificationCompat.Builder builder=new NotificationCompat.Builder(MainActivity.this )
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("啊哦!你有"+count+"条新消息")
.setContentText("你已经成功创建一条通知");
Intent intent=new Intent(MainActivity.this,ResultActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pd=PendingIntent.getActivity(MainActivity.this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(pd);
NotificationManager mNotificationManager=(NotificationManager) getSystemService
(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on
mNotificationManager.notify(id, builder.build());
三)给通知加上进度条
private NotificationManager mNotificationManager;
private NotificationCompat.Builder builder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
count++;
mNotificationManager=(NotificationManager) getSystemService
(Context.NOTIFICATION_SERVICE);
builder=new NotificationCompat.Builder(MainActivity.this )
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("啊哦!你有"+count+"条新消息")
.setContentText("你已经成功创建一条通知");
new Thread(
new Runnable() {
@Override
public void run() {
int incr;
// Do the "lengthy" operation 20 times
for (incr = 0; incr <= 100; incr+=5) {
// Sets the progress indicator to a max value, the
// current completion percentage, and "determinate"
// state
builder.setProgress(100, incr, false);
// Displays the progress bar for the first time.
mNotificationManager.notify(0, builder.build());
// Sleeps the thread, simulating an operation
// that takes time
try {
// Sleep for 5 seconds
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
// When the loop is finished, updates the notification
builder.setContentText("Download complete")
// Removes the progress bar
.setProgress(0,0,false);
mNotificationManager.notify(id, builder.build());
}
}
// Starts the thread by calling the run() method in its Runnable
).start();
}
});
}