Android notification bar 如何使通知即使在设备关闭然后重新启动后仍保留在通知栏中?
即使在设备重新启动后,我也需要将通知保存在通知栏中。在设备重新启动时显示通知您可以使用以下方法。系统重新启动时,通知托盘变空。即使在系统关闭时,也无法发出留在内存中的通知。显示持久通知的唯一方法是将数据存储在数据库或共享首选项等位置,并在系统重新启动时显示相同的通知 只需在OnReceive方法中编写代码,设备重新启动时将调用该方法 首先,您需要在清单中定义一个操作名为android.intent.action.BOOT_的接收者Android notification bar 如何使通知即使在设备关闭然后重新启动后仍保留在通知栏中?,android-notification-bar,Android Notification Bar,即使在设备重新启动后,我也需要将通知保存在通知栏中。在设备重新启动时显示通知您可以使用以下方法。系统重新启动时,通知托盘变空。即使在系统关闭时,也无法发出留在内存中的通知。显示持久通知的唯一方法是将数据存储在数据库或共享首选项等位置,并在系统重新启动时显示相同的通知 只需在OnReceive方法中编写代码,设备重新启动时将调用该方法 首先,您需要在清单中定义一个操作名为android.intent.action.BOOT_的接收者 PendingIntent pendingIntent
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int)(Math.random() * 100),MyIntent,PendingIntent.FLAG_UPDATE_CURRENT);
//A PendingIntent will be fired when the notification is clicked. The FLAG_CANCEL_CURRENT flag cancels the pendingintent
mNotification.setLatestEventInfo(getApplicationContext(), MyNotificationTitle, MyNotificationText,pendingIntent);
mNotification.flags |= Notification.FLAG_NO_CLEAR;
notificationManager.notify(0, mNotification);
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MainActivity.this.finish();
startActivity(intent);
还要确保包含已完成的启动权限
<!-- Start the Service if applicable on boot -->
<receiver android:name="com.prac.test.ServiceStarter">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.util.Log;
公共类ServiceStarter扩展广播接收器{
@凌驾
公共void onReceive(上下文、意图){
/*在此处显示通知*/
}
}
这是完整的代码。我只是想问一下,在设备重新启动后,是否有任何标志设置可以使通知保持在bar中?谢谢您的回答。但这不会使通知持久存在。您可以在此处启动新通知,但不能启动上一个通知。
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class ServiceStarter extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
/* show notification here*/
}
}