彷徨的青蛙 · 县移民局聚焦“四风”找问题_党建工作· 7 月前 · |
失恋的马铃薯 · 二十大这样说丨闽宁协作结硕果 海原劲吹“蘑菇 ...· 1 年前 · |
坚强的橙子 · 中国能源报 - 关注· 1 年前 · |
大方的稀饭 · 从肖之东同志先进事迹谈共产党员的廉洁自律和担当作为· 1 年前 · |
独立的滑板 · 如何看待漫画《死神 Bleach》20 ...· 1 年前 · |
我有一个Android的前台服务设置。我想要更新通知文本。我正在创建如下所示的服务。
如何更新在此前台服务中设置的通知文本?更新通知的最佳实践是什么?任何示例代码都将不胜感激。
public class NotificationService extends Service {
private static final int ONGOING_NOTIFICATION = 1;
private Notification notification;
@Override
public void onCreate() {
super.onCreate();
this.notification = new Notification(R.drawable.statusbar, getText(R.string.app_name), System.currentTimeMillis());
Intent notificationIntent = new Intent(this, AbList.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
this.notification.setLatestEventInfo(this, getText(R.string.app_name), "Update This Text", pendingIntent);
startForeground(ONGOING_NOTIFICATION, this.notification);
}
我在我的主活动中创建服务,如下所示:
// Start Notification Service
Intent serviceIntent = new Intent(this, NotificationService.class);
startService(serviceIntent);
我认为使用相同的惟一ID和使用新信息的
Notification
再次调用
startForeground()
是可行的,尽管我还没有尝试过这种情况。
更新:根据评论,您应该使用NotifcationManager更新通知,您的服务将继续停留在前台模式。看看下面的答案。
改进了安卓8.0+中的Luca Manzo answer,当更新通知时,它将发出声音并显示为平视。
为了防止出现这种情况,您需要添加
setOnlyAlertOnce(true)
所以代码是:
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){
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
((NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(
NotificationChannel("timer_notification","Timer Notification",NotificationManager.IMPORTANCE_HIGH))
// The PendingIntent to launch our activity if the user selects
// this notification
PendingIntent contentIntent=PendingIntent.getActivity(this,
0,new Intent(this,MyActivity.class),0);
return new NotificationCompat.Builder(this,"my_channel_01")
.setContentTitle("some title")
.setContentText(text)
.setOnlyAlertOnce(true) // so when data is updated don't make sound and alert in android 8.0+
.setOngoing(true)
.setSmallIcon(R.drawable.ic_launcher_b3)
.setContentIntent(contentIntent)
.build();
* 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);
}
似乎没有一个现有的答案显示了如何处理完整的情况-如果是第一次调用,则发送给startForeground,但会更新后续调用的通知。
您可以使用以下模式来检测正确的情况:
private void notify(@NonNull String action) {
boolean isForegroundNotificationVisible = false;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
StatusBarNotification[] notifications = notificationManager.getActiveNotifications();
for (StatusBarNotification notification : notifications) {
if (notification.getId() == FOREGROUND_NOTE_ID) {
isForegroundNotificationVisible = true;
break;
Log.v(getClass().getSimpleName(), "Is foreground visible: " + isForegroundNotificationVisible);
if (isForegroundNotificationVisible){
notificationManager.notify(FOREGROUND_NOTE_ID, buildForegroundNotification(action));
} else {
startForeground(FOREGROUND_NOTE_ID, buildForegroundNotification(action));
}
此外,您还需要像在其他答案中一样构建通知和通道:
private Notification buildForegroundNotification(@NonNull String action) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel();
//Do any customization you want here
String title;
if (ACTION_STOP.equals(action)) {
title = getString(R.string.fg_notitifcation_title_stopping);
} else {
title = getString(R.string.fg_notitifcation_title_starting);
//then build the notification
return new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setOngoing(true)
.build();
@RequiresApi(Build.VERSION_CODES.O)
private void createNotificationChannel(){
NotificationChannel chan = new NotificationChannel(CHANNEL_ID, getString(R.string.fg_notification_channel), NotificationManager.IMPORTANCE_DEFAULT);