【问题标题】:Send ArrayList<String> from BroadCastReceiver to Activity将 ArrayList<String> 从 BroadCastReceiver 发送到 Activity
【发布时间】:2012-03-12 19:20:29
【问题描述】:

场景:

  • 我有一个警报计划在指定的时间内运行。每次执行时,我的 BroadCastReceiver 都会触发。

  • 在 BroadCastReceiver 中,我进行了各种检查,最终得到一个纯字符串的 ArrayList

  • 我在状态栏上显示通知

  • 当用户点击通知时,我会显示一个活动。我需要在我的 Activity 中使用 ArrayList 将其显示在视图上。

这里是示例代码:

public class ReceiverAlarm extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
         ArrayList<String> notifications = new ArrayList<String>();

         //do the checks, for exemplification I add these values
         notifications.add("This is very important");
         notifications.add("This is not so important");
         notifications.add("This is way too mimportant");

         NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
         //init some values from notificationManager
         Intent intentNotif = new Intent(context, NotificationViewer.class);
         PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intentNotif, 0);

         Notification notification = new Notification(icon, text, when);
         notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
         notificationManager.notify(NOTIFICATION_ID, notification);
    }

还有

   public class NotificationViewer extends Activity {


            @Override
            protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.notification_viewer);

                   //HERE I NEED the ArrayList<String> notifications
    }

我尝试了我发现的大部分东西,从捆绑包到 putStringArrayListExtra(),但没有任何效果。在我的活动中,我找不到检索数据的方法。 请帮助我,因为我被卡住了。

【问题讨论】:

  • 你是在 intentNotif 还是 contentIntent 上设置 extras 吗?
  • 我在 intentNotif 上尝试过的附加功能

标签: android android-activity arraylist broadcastreceiver bundle


【解决方案1】:

根据标记为解决方案HERE 的答案,如果您没有为待处理的意图指定操作,则不会传播额外内容

【讨论】:

    【解决方案2】:

    根据我的建议,你可以通过两种方式快速收到:

    1. 创建实现Percelable 的自定义类,并设计您自己的实现来写入和读取parcelable 对象。
    2. 使用Gson 将对象序列化为单个 json 字符串,将其包装在 Intent 中,在另一端接收它并将 Json 字符串反序列化为所需的对象类型

    可能还有其他一些方法,例如将 ArrayList 序列化为字节并将其写入文件并稍后读取,但这两种是我可以推荐您处理任何类型信息的最佳方法。就个人而言,我喜欢第二种,使用 Gson 让它自己处理所有事情。

    【讨论】:

      【解决方案3】:

      基于所有 cmets,工作解决方案如下:

      在广播接收器上

      Intent intentNotif = new Intent(context, NotificationViewer.class);
      intentNotif.putStringArrayListExtra("list", notifications);
      

      关于活动

      Bundle b = getIntent().getExtras();
      if (b != null) {
          testArrayList= b.getStringArrayList("list");
      }
      

      这似乎工作得很好。

      【讨论】:

      • 啊,好点子。我以为putStringArrayListExtra 只能在后来的 API 中使用——我显然在想一些完全不同的东西。
      猜你喜欢
      • 2012-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多