重启一次后通知运行

时间:2014-05-07 13:49:22

标签: java android

我想在重启后通知工作。我提醒开始,但一次。如果我然后改变手机上的日期 - 那么没有通知。只有再次运行应用程序,它们才会。也就是说,重新启动手机而不启动应用程序通知只会出现一次 - 当您启动手机时。

我在 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 and Service):

<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>

1 个答案:

答案 0 :(得分:0)

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

请检查此主题:

Repeating Alarm Manager After reboot