onReceiver of BroadcastReceiver not called, AlarmM

2019-01-20 09:55发布

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

3条回答
看我几分像从前
2楼-- · 2019-01-20 10:44

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>
查看更多
女痞
3楼-- · 2019-01-20 10:52

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);
查看更多
老娘就宠你
4楼-- · 2019-01-20 10:56

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

查看更多
登录 后发表回答