【问题标题】:Broadcast receiver throws error after reboot when application was not opened before之前未打开应用程序时,广播接收器在重新启动后引发错误
【发布时间】:2011-12-01 00:12:01
【问题描述】:

我有一个广播接收器,在重启后启动以在警报管理器的帮助下设置多个警报。 重启后的广播和接收工作正常,但是在此广播中设置的警报在触发时会引发错误。

E/AndroidRuntime(4330):     at mobileTechnology.geoCalendar.SetAlarm.checkReminder(SetAlarm.java:82)
E/AndroidRuntime(4330):     at mobileTechnology.geoCalendar.SetAlarm.access$1(SetAlarm.java:80)
E/AndroidRuntime(4330):     at mobileTechnology.geoCalendar.SetAlarm$1.gotLocation(SetAlarm.java:73)
E/AndroidRuntime(4330):     at mobileTechnology.geoCalendar.CurrentLocation$2.onLocationChanged(CurrentLocation.java:70)

当我在重新启动后在其他警报触发之前打开应用程序时,不会出现错误。 你有什么想法,为什么应用程序会这样?在广播接收器中,我正在访问一个普通的 java 类(无活动)以通过回调确定当前位置。 之前没有打开应用,访问这个类有问题吗?

编辑:我猜我的清单文件配置错误,因为缺少 Intent 过滤器。谁能告诉我正确的做法是什么?

<receiver android:name="SetAlarm"></receiver>

EDIT2:

谢谢,这是我项目的更多代码:

在运行期间,使用警报管理器为事件设置多个警报。闹钟是通过调用来创建的

new SetAlarm (Context context, Bundle bundle, Long time_how_far_in_advance_alarm_should_be_fired)

该包包含有关事件的详细信息(名称、时间...),并且 SetAlarm 构造函数如下所示:

AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, SetAlarm.class);
intent.putExtra("data", bundle);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, bundle.getInt("id"), intent,PendingIntent.FLAG_UPDATE_CURRENT);

Calendar cal = Calendar.getInstance();
*<set time for reminder>*
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                  pendingIntent);

接收者与广播属于同一类,由

定义
<receiver android:name="SetAlarm"></receiver>

到目前为止,一切正常,因为警报会在应该触发的时候触发,并且会显示通知。

要在重启后重新加载警报,还有另一个类 ReloadAlarms,从数据库中加载所有事件并再次设置警报。

public void onReceive(Context context, Intent intent)

<receiver android:name="ReloadAlarms" >
        <intent-filter >
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
</receiver>

通过调用 SetAlarm 并传递刚刚从第一个 on receive 方法接收到的上下文来再次设置警报。 SetAlarm(Context context, Bundle bundle, 长提醒)

只要调用 SetAlarm 中的 onReceive 方法,它就会验证是否必须将警报设置为另一个时间。如果是,我打电话 新 SetAlarm(_context, newBundle, newTime); _context 又是我刚刚从 onReceive 方法收到的上下文。

问题是 onReceive 方法被连续调用。 (至少在需要重新安排活动的情况下,即使没有重新启动)

onReceive 方法如何识别警报何时触发。是否有可能发生这种情况,因为我将 onReceive 方法收到的上下文传递给 new SetAlarm(...) 以进行重新安排?

结束编辑2

感谢您的帮助!

【问题讨论】:

  • 如果您发布该异常堆栈跟踪的其余部分会很有帮助,因为看起来您只是复制了其中的一部分。前几行很重要
  • @Craigy 我真的只得到这 4 行作为我的错误输出。在我的问题中添加了更多信息。

标签: android broadcastreceiver


【解决方案1】:

如果您需要我们的帮助,您需要显示更多代码...我们现在在黑暗中工作...尝试将其作为意图放在清单中: <intent-filter > <action android:name="android.intent.action.BOOT_COMPLETED"/> <category android:name="android.intent.category.HOME"/> </intent-filter>

【讨论】:

  • 我在我的问题中添加了更多信息。对于我的重启广播接收器,我已经添加了启动完成操作。
【解决方案2】:

好的,我终于找到了错误...问题是因为我在重启后没有初始化我的设置单例。我实现了一个在我的主要活动中调用的 initializeSetting 函数。我认为这是因为广播接收器一直在执行....

现在我也在广播接收器中初始化设置(如果尚未发生)并且它可以工作。

愚蠢的错误让我花了好几个小时 -.-

【讨论】:

  • 很高兴您找到答案...下次从头开始添加更多代码,因为我们不知道您的应用程序中发生了什么...祝您好运!!! :D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-26
相关资源
最近更新 更多