use*_*751 5 notifications android
我只是想知道AndroidNotificationManager.notify和Android 之间有什么区别startForeground。
小智 3
使用NotificationManager.notify您可以根据需要发布任意数量的通知更新,包括对进度条的调整,通过Noticiation.Builder.setProgress这种方式,您只向用户显示一个通知,并且它是startForeground.
当你想更新startForeground()设置的Notification时,只需构建一个新的notification,然后使用NotificationManager来通知它。
关键点是使用相同的通知 ID。
我没有测试重复调用startForeground()更新Notification的场景,但我认为使用NotificationManager.notify会更好。
更新通知不会将服务从前台状态中删除(这只能通过调用stopForground.
这是一个例子:
private static final int notif_id=1;
@Override
public void onCreate (){
this.startForeground();
}
private void startForeground() {
startForeground(notif_id, getMyActivityNotification(""));
}
private Notification getMyActivityNotification(String text){
// The PendingIntent to launch our activity if the user selects
// this notification
CharSequence title = getText(R.string.title_activity);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MyActivity.class), 0);
return new Notification.Builder(this)
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.ic_launcher_b3)
.setContentIntent(contentIntent).getNotification();
}
/**
* this is the method that can be called to update the Notification
*/
private void updateNotification() {
String text = "Some text that will update the notification";
Notification notification = getMyActivityNotification(text);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notif_id, notification);
}
Run Code Online (Sandbox Code Playgroud)
您可以在此处找到更多示例和说明NotificationManager.notify
我还建议您参考此页面以了解更多信息startForeground
的用法可以在这里startForeground找到
| 归档时间: |
|
| 查看次数: |
1934 次 |
| 最近记录: |