onReceiver of BroadcastReceiver not called, AlarmManager

后端 未结 4 1658
春和景丽
春和景丽 2020-12-10 14:35

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

4条回答
  •  执笔经年
    2020-12-10 15:10

    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

提交回复
热议问题