[英]AlarmManager for BroadcastReceiver in Android
I have been trying to repeat an action using AlarmManager but, it works fine once and then doesn't repeat again and again after 20 seconds. 我一直在尝试使用AlarmManager重复执行一项操作,但是它一次可以正常工作,而在20秒后不会一次又一次重复。
public class CheckingService extends Service {
private static final String APP_TAG = "com.test";
@Override
public IBinder onBind(final Intent intent) {
return null;
}
@Override
public int onStartCommand(final Intent intent, final int flags,
final int startId) {
Log.d(APP_TAG, "event received in service: " + new Date().toString());
Toast.makeText(CheckingService.this, "TEST", Toast.LENGTH_SHORT).show();
return Service.START_NOT_STICKY;
}
}
BroadcastReceiver to schedule an Alarm: BroadcastReceiver计划警报:
public class SchedulerReciever extends BroadcastReceiver {
public SchedulerReciever() {
}
private static final String APP_TAG = "com.test";
private static final int EXEC_INTERVAL = 20 * 10;
@Override
public void onReceive(final Context ctx, final Intent intent) {
Log.d(APP_TAG, "onReceive() called");
AlarmManager alarmManager = (AlarmManager) ctx
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(ctx, MyBraoadCastReciever.class); // explicit
// intent
PendingIntent intentExecuted = PendingIntent.getBroadcast(ctx, 0, i,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar now = Calendar.getInstance();
now.add(Calendar.SECOND, 2);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
now.getTimeInMillis(), EXEC_INTERVAL, intentExecuted);
}
}
My broadcast receiver that loads the service: 我加载服务的广播接收器:
public class MyBraoadCastReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent eventService = new Intent(context, CheckingService.class);
context.startService(eventService);
}
}
In the last my AndroidManifest.xml which is using startup 在我最后一个使用启动的AndroidManifest.xml中
<receiver android:name=".SchedulerReciever" android:process=":my_process">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<service android:name=".CheckingService" android:process=":my_process" >
</service>
<receiver
android:name=".MyBraoadCastReciever" android:process=":my_process"
android:enabled="true"
android:exported="false" >
</receiver>
Your EXEC_INTERVAL
is set to 200ms instead of 20 * 1000 (20s). 您的EXEC_INTERVAL
设置为200ms,而不是20 * 1000(20s)。
Also if you use setRepeating, better to use a relative time AlarmManager.ELAPSED_REALTIME_WAKEUP
which is a time since the phone boot, and instead of creating a calendar, you can just call SystemClock.elapsedRealtime()
or with an optional offset of 2s SystemClock.elapsedRealtime() + 2000
. 另外,如果使用setRepeating,最好使用相对时间AlarmManager.ELAPSED_REALTIME_WAKEUP
,这是自电话启动以来的时间,而不是创建日历,您可以仅调用SystemClock.elapsedRealtime()
或使用2s的可选偏移量SystemClock.elapsedRealtime() + 2000
。
So your set repeating should looks like: 因此,您的重复设置应如下所示:
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 2000, EXEC_INTERVAL, intentExecuted);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.