[英]send extras to broadcast reciever from Activity
My application is about Reminders. 我的应用程序是关于提醒的。 After inserting the reminders into the SQLite database it is showing ID's correctly in the AddReminderActivity.java class correctly but it is returning ID=1 in the AlarmReciver.java class in the following code.
将提醒插入SQLite数据库后,它在AddReminderActivity.java类中正确显示了ID,但在以下代码中在AlarmReciver.java类中返回了ID = 1。
This is how I am putting extra 这就是我的额外投入
Intent intent = new Intent(this, AlarmReciever.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("reminderId", reminderId);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(this.getApplicationContext(), 5, intent, 0);
AlarmManager mAlarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
assert mAlarmManager != null;
mAlarmManager.set(AlarmManager.RTC_WAKEUP, milliSecondsFromDate, pendingIntent);
Toast.makeText(this, "Alarm set at " + selectedDate + " " + timeInHrs, Toast.LENGTH_LONG).show();
finish();
This is how i am getting extra 这就是我越来越
@Override
public void onReceive(Context context, Intent intent) {
Long reminderId = intent.getLongExtra("reminderId", -1);
mDatabaseHelper = new DatabaseHelper(context);
Reminder reminder = mDatabaseHelper.getReminder(reminderId);
Intent serviceIntent = new Intent(context, TTSService.class);
if (reminder != null && !reminder.getText().isEmpty()) {
serviceIntent.putExtra("reminderText", reminder.getText());
}
context.startService(serviceIntent);
Toast.makeText(context, "AlarmReciever..", Toast.LENGTH_LONG).show();
}
Use This to getting extra 使用它来获得额外的
@Override
public void onReceive(Context context, Intent intent) {
Long reminderId = intent.getExtras().getLong("reminderId", -1);
mDatabaseHelper = new DatabaseHelper(context);
Reminder reminder = mDatabaseHelper.getReminder(reminderId);
Intent serviceIntent = new Intent(context, TTSService.class);
if (reminder != null && !reminder.getText().isEmpty()) {
serviceIntent.putExtra("reminderText", reminder.getText());
}
context.startService(serviceIntent);
Toast.makeText(context, "AlarmReciever..", Toast.LENGTH_LONG).show();
}
In order for the "extras" to be set correctly, you need to use PendingIntent.FLAG_UPDATE_CURRENT
in the call to getBroadcast()
as follows: 为了正确设置“ extras”,您需要在对
getBroadcast()
的调用中使用PendingIntent.FLAG_UPDATE_CURRENT
,如下所示:
PendingIntent pendingIntent =
PendingIntent.getBroadcast(this.getApplicationContext(), 5, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Also, are you sure that reminderId
is of type long
or Long
in the code where you call putExtra()
? 另外,你确定
reminderId
的类型是long
或Long
在您调用代码putExtra()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.