我有一段非常简单的代码,用于安排下一次同步操作。它在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)
答案 0 :(得分:0)
您的PendingIntent.getBroadcast
和alarm.set
使用请求REQUEST_SYNC_ALL
我在Alarm doc和PendingIntent doc中找不到该常数。
也许我与最新的SDK不同步:)