现在的位置: 首页 > 综合 > 正文

【notification】Android 中创建状态栏通知

2017年10月29日 ⁄ 综合 ⁄ 共 709字 ⁄ 字号 评论关闭
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(android.R.drawable.btn_default, "新的通知", System.currentTimeMillis());     // 通过构造函数设定通知的图标与标题
Intent intentNoti = new Intent(Intent.ACTION_VIEW);    // 可以使用其他的意图,以实现希望的功能
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intentNoti, 0);
notification.setLatestEventInfo(getApplicationContext(), "通知的标题", "通知的内容", contentIntent);    // 设定通知的标题与内容,超出一行的部分将被省略
notificationManager.notify(notificationID, notification);   // 生成 ID 为 notificationID 的通知。若该 ID 已存在,则更新已有通知的内容

 

说明:NotificationManager 可以通过 getSystemService(NOTIFICATION_SERVICE) 方法取得。它本身还有一些其他的便利方法:cancel(int id)删除指定 id 的通知。cancelAll()删除本应用程序发出的所有通知。

抱歉!评论已关闭.