为什么这个具有待定意图的Android警报会崩溃?

时间:2011-01-21 06:49:22

标签: android alarmmanager android-pendingintent android-syncadapter

我有一段非常简单的代码,用于安排下一次同步操作。它在SyncAdapter内部运行,它扩展了AbstractThreadedSyncAdapter - 因此使用它的上下文。 PendingIntent是为BroadcastReceiver“MyService”启动的,它只管理SyncAdapter。

我只是想把它安排在24小时内重复。

private void scheduleNextSync(Long timeFromNow) {
    Intent intentSync = new Intent(getContext(), MyService.class);
    intentSync.setAction(Intent.ACTION_SYNC);
    intentSync.addCategory("android.provider.CONTACTS_STRUCTURE");
    intentSync.putExtra(MySyncAdapter.EXTRA_SYNC_TYPE, "");
    intentSync.putExtra(MySyncAdapter.EXTRA_RESOLVE_METAID, false);

    int flags = 0;
    int request = REQUEST_SYNC_ALL;
    PendingIntent piSync = PendingIntent.getBroadcast(getContext(), request, intentSync, flags);
    //
    Log.i(TAG, "schedulePendingSync: " + piSync.toString());
    AlarmManager alarm = (AlarmManager) getContext().getSystemService(Activity.ALARM_SERVICE);
    alarm.set(request, SystemClock.elapsedRealtime() + timeFromNow, piSync);
}

日志输出:

01-20 22:42:18.864 W/dalvikvm(14447): threadid=8: thread exiting with uncaught exception (group=0x40020950)
01-20 22:42:18.919 E/AndroidRuntime(14447): FATAL EXCEPTION: Thread-9
01-20 22:42:18.919 E/AndroidRuntime(14447): java.lang.NullPointerException
01-20 22:42:18.919 E/AndroidRuntime(14447):     at android.os.Parcel.readException(Parcel.java:1253)
01-20 22:42:18.919 E/AndroidRuntime(14447):     at android.os.Parcel.readException(Parcel.java:1235)
01-20 22:42:18.919 E/AndroidRuntime(14447):     at android.app.IAlarmManager$Stub$Proxy.set(IAlarmManager.java:174)
01-20 22:42:18.919 E/AndroidRuntime(14447):     at android.app.AlarmManager.set(AlarmManager.java:139)
01-20 22:42:18.919 E/AndroidRuntime(14447):     at com.mysyncadpter.android.MySyncAdapter.scheduleNextSync(MySyncAdapter.java:145)
01-20 22:42:18.919 E/AndroidRuntime(14447):     at com.mysyncadpter.android.MySyncAdapter.access$2(MySyncAdapter.java:131)
01-20 22:42:18.919 E/AndroidRuntime(14447):     at com.mysyncadpter.android.MySyncAdapter$1.run(MySyncAdapter.java:123)
01-20 22:42:18.919 E/AndroidRuntime(14447):     at java.lang.Thread.run(Thread.java:1096)

1 个答案:

答案 0 :(得分:0)

您的PendingIntent.getBroadcastalarm.set使用请求REQUEST_SYNC_ALL 我在Alarm docPendingIntent doc中找不到该常数。

也许我与最新的SDK不同步:)