我有一段非常简单的代码,用于安排我的下一次同步操作。它在扩展 AbstractThreadedSyncAdapter 的 SyncAdapter 内运行——因此使用它的上下文。它为仅管理 SyncAdapter 的 BroadcastReceiver“MyService”启动的 PendingIntent。
我只是想安排它在 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)