【问题标题】:Broadcast Receiver in a remote process远程进程中的广播接收器
【发布时间】:2013-08-29 15:31:49
【问题描述】:

我的应用程序具有设置提醒的功能。我正在创建这样的提醒

           Intent intent = new Intent(Context,ReminderActivity.class);

    intent.putExtra(ResolutionsListActivity.RESOLUTION_OBJECT, resolution);

    PendingIntent sender = PendingIntent.getBroadcast(
    ComposeResolutionActivity.this,intent,PendingIntent.FLAG_UPDATE_CURRENT);

          AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            interval, sender);

分辨率 --- 可序列化的对象。

这里我的 ReminderActivity 类扩展了广播接收器。

我像这样在清单中声明了我的接收器

       < receiver android:name="com.webileapps.resolutions.ReminderActivity"
        android:process=":remote" />

在我的提醒活动类中,我正在捕捉触发的警报并显示通知。

    public class ReminderActivity extends BroadcastReceiver {
private static final String TAG = "ReminderActivity";

@Override
public void onReceive(Context context, Intent intent) {
    // Toast.makeText(context, "Alarm set", Toast.LENGTH_SHORT).show();
    Resolution resolution = (Resolution) intent
                                    .getSerializableExtra(ResolutionsListActivity.RESOLUTION_OBJECT);

        showNotification(context, resolution);
}

我正在传递可序列化对象,以便 - 每当用户点击提醒通知时 - 我可以使用该对象加载我的活动。 但是,每当警报触发时,我都会收到诸如“无法填写额外内容”之类的错误。

谁能指出我做错了什么?

【问题讨论】:

  • 你为什么要这样做,你想达到什么目的?
  • @ankit 我编辑了我的问题
  • 你为什么使用android:process=":remote"
  • #CommonsWare:我刚刚关注了 Api 演示警报示例。他们在那里为警报广播接收器声明了 android:process=":remote"
  • @CommonsWare(实际上不是给你的,Mark,但注意到我在这里时 Handroid 在回复你时没有使用 at 符号)。 Handroid...看到这个:remote clarification

标签: android broadcastreceiver


【解决方案1】:

我也有这个问题,收到了广播,但总是有“Failure fill in extras”的警告。

解决方案是将您的 Parcelable 包装在一个 Bundle 中,并将 Bundle 作为额外的添加到 PendingIntent。

问题在于,在执行 PendingIntent 时,它需要将 Intent Extras 复制到另一个 Intent,但是要复制 Parcelables,它需要再次解包和打包,并且在解包时它使用默认的 ClassLoader 而不是您的类 ClassLoader,所以它找不到要拆包的类。但是如果你包裹在一个 Bundle 中,它会将整个 Bundle 复制到 Intent 中,并且不会进行拆包。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    相关资源
    最近更新 更多