为什么这个带有待处理意图的 Android 警报会崩溃?
我有一段非常简单的代码,用于安排下一个同步操作。它在扩展 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)
I have a very simple piece of code that is used to schedule my next sync operation. It is running inside the SyncAdapter that extends AbstractThreadedSyncAdapter -- thus uses its context. The PendingIntent it started for the BroadcastReceiver "MyService" which only manages the SyncAdapter.
I am just trying to schedule it to recur in 24 hours.
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);
}
Log output:
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
PendingIntent.getBroadcast
和alarm.set
使用请求REQUEST_SYNC_ALL
我在 警报文档,也不是 PendingIntent 文档。
也许我与最新的 SDK 不同步:)
Your
PendingIntent.getBroadcast
andalarm.set
use the requestREQUEST_SYNC_ALL
I can't find that constant in the Alarm doc, nor the PendingIntent doc.
Maybe I am out of sync with the latest SDK :)