简体   繁体   中英

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.

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:

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

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

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 .

So your set repeating should looks like:

alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + 2000, EXEC_INTERVAL, intentExecuted);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM