使用 Intent 将数据从 Activity 传递到广播接收器
Pass data from Activity to Broadcast Receiver with Intent
我正在通过 BroacastReciever
在特定时间安排通知。我想将数据从通知点击传递到 activity 的 newIntent
方法。到目前为止,随着通知的通过,一切正常。但是,我在将数据从 activity
传递到我的 BrocastReceiver
class 时遇到问题。
问题是我传递的变量存在于我的 activity
class 中,而不是 broadcastReceiver
中。我不知道如何将这些数据输入我的接收器 class。
Activity.java
private void setTimer(String place){
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, BroadcastAlarm.class);
intent.setAction("MY_NOTIFICATION_MESSAGE");
intent.putExtra("place", place); //this doesn't send data to notification. I want to send place string to notification
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
BroadcastReceiver.java
@Override
public void onReceive(Context context, Intent intent) {
Intent intent1 = new Intent(context, SearchActivity.class);
intent1.putExtra("place", stringNotHere); //if I send data from here it gets available but the problem is that my place data is on my activity class not here
intent1.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notification= new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
notification.setContentIntent(pendingIntent)
}
如您所见,我需要让 activity 的 place
变量在 BroadcastReceiver
上可用。我该怎么做?
替换:
intent1.putExtra("place", stringNotHere);
有了这个:
String place = intent.getStringExtra("place");
intent1.putExtra("place", place);
我正在通过 BroacastReciever
在特定时间安排通知。我想将数据从通知点击传递到 activity 的 newIntent
方法。到目前为止,随着通知的通过,一切正常。但是,我在将数据从 activity
传递到我的 BrocastReceiver
class 时遇到问题。
问题是我传递的变量存在于我的 activity
class 中,而不是 broadcastReceiver
中。我不知道如何将这些数据输入我的接收器 class。
Activity.java
private void setTimer(String place){
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, BroadcastAlarm.class);
intent.setAction("MY_NOTIFICATION_MESSAGE");
intent.putExtra("place", place); //this doesn't send data to notification. I want to send place string to notification
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
BroadcastReceiver.java
@Override
public void onReceive(Context context, Intent intent) {
Intent intent1 = new Intent(context, SearchActivity.class);
intent1.putExtra("place", stringNotHere); //if I send data from here it gets available but the problem is that my place data is on my activity class not here
intent1.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 100, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notification= new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
notification.setContentIntent(pendingIntent)
}
如您所见,我需要让 activity 的 place
变量在 BroadcastReceiver
上可用。我该怎么做?
替换:
intent1.putExtra("place", stringNotHere);
有了这个:
String place = intent.getStringExtra("place");
intent1.putExtra("place", place);