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);
即使设备重新启动,我也需要在通知栏中保存通知。
答案 0 :(得分:1)
显示设备重启时的通知您可以使用以下方法。系统重新启动时,通知托盘变空。即使系统关闭,也无法在内存中发出此类通知。显示持久通知的唯一方法是将数据存储在数据库或共享首选项等某些位置,并在系统重新启动时显示相同的通知。
Android BroadcastReceiver on startup - keep running when Activity is in Background
只需在onRecieve方法中编写代码,该方法将在设备重新启动时调用。
首先您需要在清单中使用操作名称android.intent.action.BOOT_COMPLETED定义接收器。
<!-- 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
&GT; 确保还包括已完成的引导权限。
<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*/
}
}