当应用程序未打开之前,广播接收器在重新启动后抛出错误

时间:2011-12-01 00:12:02

标签: android broadcastreceiver

我有一个广播接收器,在重启后启动,在闹钟管理器的帮助下设置多个闹钟。 重启后的广播和接收工作完美,但是在此广播中设置的警报在被解雇时会引发错误。

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,long reminder)

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

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

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

END Edit2

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

好吧,我终于找到了错误......问题是因为我没有在重启后初始化我的设置单例。我实现了一个initializeSetting函数,在我的main活动中调用。我认为这是因为有史以来广播接收机的实施......

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

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

相关问题