android Notification的使用

来源:互联网 发布:软件开发证书含金量 编辑:程序博客网 时间:2024/06/05 16:46

手机的状态栏里出现的消息即是Notification,现在简单介绍下它的使用方法:

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

/**

创建一个PendingIntent,和Intent类似,不同的是由于不是马上调用,需要在下拉状态条出发的activity,所以采用的是PendingIntent,即点击Notification跳转启动到哪个Activity

*/
PendingIntent intent = PendingIntent.getActivity(context, 0, new Intent(context, MyPreferenceActivity.class), 0);

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.remoteview);//自定义notification的布局


Notification notification = new Notification.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("开始下载")
.setContentTitle("title")
.setNumber(1)

.setContent(remoteViews)
.setContentIntent(intent)
.build();

notification.flags = Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
manager.notify(NOTIFICATION_FLAG, notification); //为Notification添加flag,


当要手动清除该消息时调用:

manager.cancel(NOTIFICATION_FLAG); // 清除id为NOTIFICATION_FLAG的通知  




0 0
原创粉丝点击