重新启动手机后,android警报管理器不起作用

1234567

我正在使用以下代码来提醒用户在特定时间使用有关应用程序中新更新的通知,一切正常,我们会在所需时间接收提醒

这是mainactivity.java

public void setAlarm(){
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(  MainActivity.this, 0, alarmIntent, 0);

    alarmStartTime.set(Calendar.HOUR_OF_DAY, 10);
    alarmStartTime.set(Calendar.MINUTE, 00);
    alarmStartTime.set(Calendar.SECOND, 0);
    alarmManager.setRepeating(AlarmManager.RTC, alarmStartTime.getTimeInMillis(), getInterval(), pendingIntent);
}
private int getInterval(){
     int days = 1;
     int hours = 24;
     int minutes = 60;
     int seconds = 60;
     int milliseconds = 1000;
     int repeatMS = days * hours * minutes * seconds * milliseconds;
     return repeatMS;
}

AlarmReceiver.java

public class AlarmReceiver extends BroadcastReceiver {

    NotificationManager notificationManager;

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service1 = new Intent(context, AlarmService.class);
            context.startService(service1);
    }
}

和AlarmService.java

public class AlarmService extends Service { 
   private static final int NOTIFICATION_ID = 1;
   private NotificationManager notificationManager;
   private PendingIntent pendingIntent;

    @Override
    public IBinder onBind(Intent arg0)
    {
        return null;
    }

   @SuppressWarnings("static-access")
   @Override
   public void onStart(Intent intent, int startId)
   {
       super.onStart(intent, startId);
       Context context = this.getApplicationContext();
       notificationManager = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
       Intent mIntent = new Intent(this, MainActivity.class);
    pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_CANCEL_CURRENT);     
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentTitle("Bananas");
    builder.setContentText("get your bananas");
    builder.setSmallIcon(R.drawable.ic_launcher);
    builder.setContentIntent(pendingIntent);

    notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, builder.build());
    }
}

我的问题是,如果我们重新启动电话后,先前有关警报的所有信息都丢失了,并且重新启动后下次没有收到警报,那该怎么办,这是警报管理器或通知出现问题。

莫扎

您需要将此添加到您的AndroidManifest.xml

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

 <receiver
            android:name="Your package name.AlarmReboot"
            android:enabled="true"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            </intent-filter>
        </receiver>

并添加一个广播接收器,一旦电话重启,它将重新设置警报:

public class AlarmReboot extends BroadcastReceiver{

}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android-重新启动手机后启动器应用程序中断

来自分类Dev

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

来自分类Dev

关机时重新启动手机

来自分类Dev

使用adb完成操作后是否需要重新启动手机?

来自分类Dev

在新手机上重新启动之前,Android GPS不起作用

来自分类Dev

警报管理器不起作用

来自分类Dev

重新启动后的警报管理器在意图时显示错误 NullPointerException

来自分类Dev

重新启动手机,现在xCode无法识别它

来自分类Dev

Android在广播接收器中暂停并重新启动警报管理器

来自分类Dev

手机旋转后的LinearLayoutManager setOrientation不起作用

来自分类Dev

冻结后重新启动更新管理器!

来自分类Dev

冻结后重新启动更新管理器!

来自分类Dev

手机进入飞行模式时定时器停止,离开AM后不会重新启动

来自分类Dev

警报管理器在指定时间不起作用

来自分类Dev

警报管理器似乎不起作用

来自分类Dev

遮罩的输入在android手机中不起作用?

来自分类Dev

遮罩的输入在android手机中不起作用?

来自分类Dev

重新启动后的Fstab不起作用

来自分类Dev

重新启动终端后别名不起作用

来自分类Dev

重新启动后,CUG登录在发布服务器上不起作用

来自分类Dev

重新启动后,CUG登录在发布服务器上不起作用

来自分类Dev

重新启动网络管理器

来自分类Dev

重新启动后,Android内容提供商URI不起作用

来自分类Dev

启动管理器似乎不起作用

来自分类Dev

Android Studio:“更新和重新启动”不起作用

来自分类Dev

从片段内部调用时,警报管理器BroadcastReceiver,setRepeating不起作用

来自分类Dev

日期和时间在警报管理器中不起作用

来自分类Dev

更新管理器不起作用

来自分类Dev

下载管理器不起作用

Related 相关文章

  1. 1

    Android-重新启动手机后启动器应用程序中断

  2. 2

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

  3. 3

    关机时重新启动手机

  4. 4

    使用adb完成操作后是否需要重新启动手机?

  5. 5

    在新手机上重新启动之前,Android GPS不起作用

  6. 6

    警报管理器不起作用

  7. 7

    重新启动后的警报管理器在意图时显示错误 NullPointerException

  8. 8

    重新启动手机,现在xCode无法识别它

  9. 9

    Android在广播接收器中暂停并重新启动警报管理器

  10. 10

    手机旋转后的LinearLayoutManager setOrientation不起作用

  11. 11

    冻结后重新启动更新管理器!

  12. 12

    冻结后重新启动更新管理器!

  13. 13

    手机进入飞行模式时定时器停止,离开AM后不会重新启动

  14. 14

    警报管理器在指定时间不起作用

  15. 15

    警报管理器似乎不起作用

  16. 16

    遮罩的输入在android手机中不起作用?

  17. 17

    遮罩的输入在android手机中不起作用?

  18. 18

    重新启动后的Fstab不起作用

  19. 19

    重新启动终端后别名不起作用

  20. 20

    重新启动后,CUG登录在发布服务器上不起作用

  21. 21

    重新启动后,CUG登录在发布服务器上不起作用

  22. 22

    重新启动网络管理器

  23. 23

    重新启动后,Android内容提供商URI不起作用

  24. 24

    启动管理器似乎不起作用

  25. 25

    Android Studio:“更新和重新启动”不起作用

  26. 26

    从片段内部调用时,警报管理器BroadcastReceiver,setRepeating不起作用

  27. 27

    日期和时间在警报管理器中不起作用

  28. 28

    更新管理器不起作用

  29. 29

    下载管理器不起作用

热门标签

归档