android Notification
是显示在手机状态栏上的通知,具备全局效果的通知
时效性不强
步骤:
// 得到系统服务
NotificationManager mNotificationManager
= (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
// 建立一个Notification对象
Notification notification = new Notification(R.drawable.ic_launcher,
"短信来了", System.currentTimeMillis());
// 设置点击事件关联activity
Intent intent = new Intent(this, SecondActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,intent, 0);
// 设置标题跟内容
notification.setLatestEventInfo(this, "小丽", "在么", contentIntent);
// 发送通知
mNotificationManager.notify(0, notification);
// 点击以后,消失
// notification.flags = Notification.FLAG_AUTO_CANCEL;
// 点击以后,不会消失
notification.flags = Notification.FLAG_ONGOING_EVENT;
取消通知:
mNotificationManager.cancel(0);//参数必须是以前发送通知的idandroid