android中Notification的用法

来源:互联网 发布:拓词软件下载 编辑:程序博客网 时间:2024/06/09 20:02

总结4步

1 获取通知对象

2新建建Notification

3关联意图

4执行通知

案例如下


<span style="font-size:18px;"></span>
<span style="font-size:18px;">private void button1() {count++;NotificationManager manager = (NotificationManager) MainActivity.this.getSystemService(Context.NOTIFICATION_SERVICE);Notification notification = new Notification(R.drawable.ic_launcher,"我是通知", System.currentTimeMillis());notification.flags = Notification.FLAG_AUTO_CANCEL;notification.number = count;Intent it = new Intent(Intent.ACTION_CALL, Uri.parse("tel:110"));it.putExtra("name", "name" + count);PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 12, it,PendingIntent.FLAG_CANCEL_CURRENT);notification.setLatestEventInfo(this, "标题", "启动多条显示一条", pendingIntent);manager.notify(100, notification);}</span>
执行多条但是只显示一次,点击只执行一次,只显示一次与manager.notify(n, notification);中的n有关,n是固定的只显示一次,

只执行一次与PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 12, it,PendingIntent.FLAG_CANCEL_CURRENT);中的数字有关,相同只执行一次,不相同,可执行多次




其他的boutton事件,只要根据以上规律就可以做出,

自定义格式,除了一其他的自定义style一样,还有必须加载

 RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.自定义的style);  

notification.contentView = contentView;  

就可以了


0 0
原创粉丝点击