简体   繁体   English

连续方法调用未在Broadcast Receiver Android中执行

[英]Consecutive method calls not executing in Broadcast Receiver Android

I've a broadcast receiver, that gets triggered as per AlarmManager scheduled time interval. 我有一个广播接收器,它按照AlarmManager的预定时间间隔触发。

Now,lets say, I've a service named 'A' and the broadcast receiver named 'B'. 现在,让我们说,我有一个名为“ A”的服务和一个名为“ B”的广播接收器。 My AlarmManager is in service class A, from where calling of 'B' is scheduled. 我的AlarmManager在服务类A中,在该服务类中计划了“ B”的调用。

Now, when alarm time is triggered, and broadcast receiver is called, there are 2 consecutive method calls in 'B'. 现在,当触发警报时间并调用广播接收机时,“ B”中有2个连续的方法调用。 These methods are contained in class 'A', but called from 'B'. 这些方法包含在类“ A”中,但从“ B”中调用。

The issue is only one line of code gets called in 'B', and it doesn't return to call next line of code. 问题是只有一行代码在“ B”中被调用,并且不会返回到下一行代码。 I'm not getting why....please help. 我不明白为什么....请帮助。

The code is class 'A' is as following: 代码为“ A”类,如下所示:

    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: 在接收方“ B”中,如下所示:

 @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. 在类B的代码中,将执行对startPeriodicUpdate的第一个方法调用,但不会返回执行下一行方法。

Seems to be your stopPeriodicUpdate() method has return statement at the end , check once, If so remove that and make it void method. 似乎是您的stopPeriodicUpdate()方法在结尾处有return语句,请检查一次,如果是,请将其删除并将其设为void方法。

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 ) 无论如何,我建议您使用自己的自定义事件管理器来触发事件,而不是使用广播管理器来进行更频繁的更新(即使您可以尝试使用第三方库,例如Android EventBus https://github.com/greenrobot/EventBus

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM