我有一个在手机启动后启动的接收器,如下所示:
<receiver android:name=".OnBootReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
在我运行的接收器中设置这样的闹钟:
AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(context, LocationPoller.class);
i.putExtra(LocationPoller.EXTRA_INTENT,
new Intent(context, LocationReceiver.class));
i.putExtra(LocationPoller.EXTRA_PROVIDER,
LocationManager.GPS_PROVIDER);
PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime(),
PERIOD,
pi);
它工作正常,当然,除了当用户安装应用程序时,直到用户重启手机才会设置闹钟。
要解决这个问题,我需要从我的 Activity 中检查是否设置了 AlarmManager,如果没有,我需要从 Activity 中进行设置。
因此,如何检查是否已设置警报管理器。
最佳答案
您唯一可以做的就是当用户安装应用程序并第一次打开它时,您可以在 SharedPreference
中设置一个标志,告诉您是否设置了警报,如果没有设置警报。
在您的主要 Activity
中
checkin onCreate
方法
SharedPreferences spref = getSharedPreferences("TAG", MODE_PRIVATE);
Boolean alaram_set = spref.getBoolean("alarm_set_flag", false); //default is false
if(!alaram_set){
//create your alarm here then set the flag to true
SharedPreferences.Editor editor = spref.edit();
editor.putBoolean("alarm_set_flag", true); // value to store
editor.commit();
}
关于android - 如何检查警报是否已设置并正在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12214160/