为什么这个带有待处理意图的 Android 警报会崩溃?

发布于 2024-10-13 00:51:00 字数 2142 浏览 2 评论 0原文

我有一段非常简单的代码,用于安排下一个同步操作。它在扩展 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

⊕婉儿 2024-10-20 00:51:00

您的 PendingIntent.getBroadcastalarm.set 使用请求 REQUEST_SYNC_ALL
我在 警报文档,也不是 PendingIntent 文档

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

Your PendingIntent.getBroadcast and alarm.set use the request REQUEST_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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文