小编典典

通知在重启后运行一次

java

我想在重新启动后通知工作正常。我提醒开始,但一次。如果我随后更改了手机上的日期,则没有通知。只有再次运行该应用程序,它们才会运行。也就是说,在不启动应用程序的情况下重新启动电话后,启动电话时,通知仅显示一次。

我在 MainActivity中 设置了通知时间:

    Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, c1.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

MyAlarmService (onCreate):

mManager =(NotificationManager)this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
       Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);

   Notification notification = new Notification(R.drawable.ic_launcher,"Title", System.currentTimeMillis());

   intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

   PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
   notification.flags |= Notification.FLAG_AUTO_CANCEL;
   notification.setLatestEventInfo(this.getApplicationContext(), "Title", "Description", pendingNotificationIntent);

   mManager.notify(0, notification);

MyReceiver:

Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);

AndroidManifest(MyReceiver和服务):

<service android:name=".MyAlarmService"
                 android:enabled="true"/>
        <receiver android:name=".MyReceiver"
            android:enabled="true"
            android:exported="false"
            android:label="MyReceiver">
            <intent-filter >
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="android.intent.action.QUICKBOOT_POWERON"/>
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </receiver>

阅读 344

收藏
2020-11-30

共1个答案

小编典典

重新启动后,通知(AlarmManager)丢失。看来您需要在重启时重置Alarmanager setRepeating()

请检查此线程:

重新启动后重复警报管理器

2020-11-30