在回收者视图中单击一个单元时,我试图将数据发送到广播接收者。因此它可以启动计时器并在计时器中输入正确的数据后显示通知。
但是,当我将数据发送到广播接收器时,在触发通知时我会收到不正确的数据。
这是我的Adapter类中的代码,它将数据发送到public void onBindViewHolder(MyViewHolder holder, final int position)
方法中的Boradcast Reciever中
@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());
}
}
感谢您的帮助。 谢谢
答案 0 :(得分:0)
问题在于待处理的意图标志为0
。应该在FLAG_UPDATE_CURRENT
每次运行时更新值的时间。