【问题标题】:Sending data from Recycler View Adapter to BroadcastReciever将数据从 Recyclerview 适配器发送到 BroadcastReceiver
【发布时间】:2020-09-05 07:48:35
【问题描述】:

单击回收站视图中的单元格时,我试图向广播接收器发送数据。这样它就可以启动一个计时器并在计时器完成正确数据时显示通知。

但是,当我将数据发送到广播接收器时,当通知触发时,我收到了不正确的数据。

这是来自我的适配器类的代码,它将数据发送到方法 public void onBindViewHolder(MyViewHolder holder, final int position) 中的 Boradcast 接收器

                @Override
                public void onClick(View view) {
                    int ONE_SECOND = 1000;
                    Intent intent = new Intent(context, BroadcastReminder.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("name", timer.getTimer_name());
                    bundle.putString("img", timer.getTimer_img());
                    intent.putExtra("bundle", bundle);
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
                    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

                    long timeAtStart = System.currentTimeMillis();
                    long duration_s = ONE_SECOND * 10;

                    alarmManager.set(AlarmManager.RTC_WAKEUP,
                            timeAtStart + duration_s, pendingIntent);
                }
            });

这将启动服务,当通知触发时,广播接收器将从意图包中接收数据并显示通知。 (但是接收到的数据是错误的,每次都是同一个元素)

public class BroadcastReminder extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getBundleExtra("bundle");
        String timer_name = bundle.getString("name");
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "notifyLemubit")
                .setSmallIcon(icon)
                .setContentTitle("Timer Finished")
                .setContentText("Your " + timer_name + " Tree is Ready!")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
        notificationManagerCompat.notify(200, builder.build());

    }
}

感谢任何帮助。 谢谢

【问题讨论】:

    标签: android android-recyclerview broadcastreceiver


    【解决方案1】:

    问题在于待定意图标志为0。应该是FLAG_UPDATE_CURRENT 在每次运行时更新值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-29
      • 1970-01-01
      相关资源
      最近更新 更多