onReceiver of BroadcastReceiver not called, AlarmManager

后端 未结 4 1657
春和景丽
春和景丽 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:02

    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>
    
    0 讨论(0)
  • 2020-12-10 15:02

    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);
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-10 15:15

    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 讨论(0)
提交回复
热议问题