12

I am building a cab booking app, I need current location of the cab every 20 seconds.

I have defined a AlarmManager and need it to repeat itself every 20 seconds. But its not repeating itself regularly. Instead it repeated itself after 233 seconds, and just once. What am I doing wrong here ?

My HomeScreen has a inner class OnAlarmReceiver, in the onCreate of my HomeScreen I am calling AlarmManager

    AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(this, OnAlarmReceiver.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 20);
    mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
            cal.getTimeInMillis(), God.UPDATE_PENDING_INTERVAL, pi);

Inner class in HomeScreen

public class OnAlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // PullPendingRequests.acquireStaticLock(context);
        Toast.makeText(context, "Don't panik but your time is up!!!!.", Toast.LENGTH_LONG)
                .show();
        Log.d("Taxeeta:PullPendingRequets", "CallService Location");
        context.startService(new Intent(context, PullPendingRequests.class));
    }
}

My AndroidManifest file has

    <service
        android:name="com.taxeeta.support.PullPendingRequests"
        android:enabled="true"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@android:style/Theme.Light.NoTitleBar" />

    <receiver android:name=".com.taxeeta.HomeScreen.OnAlarmReceiver" />
</application>

Output of adb shell dumpsys alarm

 com.taxeeta
    51471ms running, 5248 wakeups
    5248 alarms: flg=0x4 cmp=com.taxeeta/.HomeScreen$OnAlarmReceiver

Output of adb shell dumpsys alarm | grep taxeeta

 ELAPSED_WAKEUP #7: Alarm{409303b0 type 2 com.taxeeta}
    operation=PendingIntent{408ba2d8: PendingIntentRecord{40887be8 com.taxeeta broadcastIntent}}
 com.taxeeta
    5248 alarms: flg=0x4 cmp=com.taxeeta/.HomeScreen$OnAlarmReceiver

5 Answers 5

14

To fix it, I removed the inner class OnAlarmReceiver and fixed the androidmanifest.xml file.

    <receiver
        android:name="com.taxeeta.support.OnAlarmReceiver"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.NOTIFY" />
        </intent-filter>
    </receiver>
8

If the answer above doesn't work for you then there is another way to not receive any callbacks when AlarmManager fires an expired alarm. You simply need to check this one out: by sending the wrong Intent on instantiation of PendingIntent. For example you wanted to receive a call onReceive on one of your receivers but you instantiated a PendingIntent via getActivity or getService, but what you actually meant is getReceiver.

When creating instance of PendingIntent, there are many ways to create it (getService, getActivity,getReceiver, getForegroundService:

if you want Activity the receiver of the intent then you:

PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_*);

if you want BroadcastReceiver the receiver of the intent:

PendingIntent.getReceiver(this, 0, intent, PendingIntent.FLAG_*);

if you want a foreground Service the receiver of the intent:

PendingIntent.getForegroundService(this, 0, intent, PendingIntent.FLAG_*);

if you want a Service the receiver of the intent:

PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_*);

Also, make sure you intents are pointing to the correct class. (e.g. creating intents for Activity, Service etc.). You will not receive any call if you pass wrongfully like this:

Intent intent = new Intent(this, MyReceiver.class); // You wanted receiver

// PendingIntent was created in such a way 
// you wanted this to be received by an activity. 
// you will not receive any call if you set it up like this.
PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_*); 

I also posted similar answer here.

HTH

1
  • 1
    This ^.. When using a Receiver with AlarmManager you need to use PendingIntent.getBroadcast(...) and it's worrking like magic ;)
    – homerun
    Commented Sep 3, 2019 at 8:48
1

This piece of code worked for me and make sure you have added reciever in android manifest file.

AlarmManager service = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, OnAlarmReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
                         PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
// Start 20 seconds after boot completed
cal.add(Calendar.SECOND, 20);
//
// Fetch every 20 seconds
// InexactRepeating allows Android to optimize the energy consumption
service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                       cal.getTimeInMillis(), 1000*20, pending);
3
  • 1
    Nope, does not work. OnAlarmReceiver, onReceive is also not being called.
    – Siddharth
    Commented Apr 8, 2013 at 10:04
  • 1
    Same here nothing on alarmmanager works for me. I've tried all the suggestions for this. Not sure what to do pretty stuck
    – JPM
    Commented Nov 8, 2016 at 21:39
  • That was so long ago not even sure what I needed this for. Sorry man if I remember I'll post it up.
    – JPM
    Commented Jul 24, 2019 at 21:59
1

The above solutions didn't work for me.

Additionally, registering dynamically via code did the trick:

Intent intent = new Intent();
intent.setAction("android.intent.action.NOTIFY");

//Register the receiver
context.registerReceiver(new OnAlarmReceiver(),new IntentFilter());
0

For anyone still stuck - make sure your broadcast receiver is not crashing in the background. Make sure to check your LogCat!

Not the answer you're looking for? Browse other questions tagged or ask your own question.