简体   繁体   中英

Consecutive method calls not executing in Broadcast Receiver Android

I've a broadcast receiver, that gets triggered as per AlarmManager scheduled time interval.

Now,lets say, I've a service named 'A' and the broadcast receiver named 'B'. My AlarmManager is in service class A, from where calling of 'B' is scheduled.

Now, when alarm time is triggered, and broadcast receiver is called, there are 2 consecutive method calls in 'B'. These methods are contained in class 'A', but called from 'B'.

The issue is only one line of code gets called in 'B', and it doesn't return to call next line of code. I'm not getting why....please help.

The code is class 'A' is as following:

    int interval = 60 * 1000;

    Calendar now = Calendar.getInstance();
    AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    intentLocationUpdateAlarm = new Intent(this, B.class);
    pendingIntentLocationUpdateAlarm = PendingIntent.getBroadcast(this, 1919, intentLocationUpdateAlarm, PendingIntent.FLAG_CANCEL_CURRENT);

    manager.set(AlarmManager.RTC_WAKEUP, now.getTimeInMillis()+(interval),
            pendingIntentLocationUpdateAlarm);

And in Receiver 'B', it's as following:

 @Override
 public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

    //Stop periodic updates here and then start again
    A.getInstance().stopPeriodicUpdate();

    A.getInstance().startPeriodicUpdates();
}

In code for class B, the first method call for startPeriodicUpdate executes, but it doesn't return to execute next line of method.

Seems to be your stopPeriodicUpdate() method has return statement at the end , check once, If so remove that and make it void method.

Anyway i would suggest you to use your own custom event managers to trigger the events instead of broadcast managers for more frequent updates (Even you can try third party library like Android EventBus https://github.com/greenrobot/EventBus )

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