intent.putExtra() 未决意图不起作用
intent.putExtra() in pending intent not working
我正在通过 alarmreceiver 从服务 class 传递待处理的意图。但是,在 pendingIntent 触发后,广播接收器 class 没有接收到 intent.putExtra() 信息。这是我触发 pendingIntent
的代码
Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, aint, PendingIntent.FLAG_UPDATE_CURRENT);
aint.putExtra("msg", msg);
aint.putExtra("phone", phone);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
报警接收者class在下方
public String msg, phonen;
@Override
public void onReceive(Context context, Intent intent){
Bundle extras = intent.getExtras();
msg = extras.getString("msg");
phonen = extras.getString("phone");
Log.d("onReceive", "About to execute MyTask");
Toast.makeText(context,msg, Toast.LENGTH_LONG).show();
}
从未决意图接收到的 toast 中的 msg 信息未显示。相反,显示的是空白吐司。
您必须使用 getStringExtra()
并确保字符串不为空:
Intent intent = getIntent();
msg = intent.getStringExtra("msg");
phonen = intent.getStringExtra("phone");
if(msg!=null){
Toast.makeText(context,msg,
Toast.LENGTH_LONG).show();
}
并在 PendingIntent 之前反转你的 putExtras:
Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
aint.putExtra("msg", msg);
aint.putExtra("phone", phone);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, aint, PendingIntent.FLAG_UPDATE_CURRENT);
试试这个
Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
aint.putExtra("msg", msg);
aint.putExtra("phone", phone);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(),
id,
aint,
// as stated in the comments, this flag is important!
PendingIntent.FLAG_UPDATE_CURRENT);
这是因为你是先初始化Intent然后添加到PendingIntent。之后,您将有意添加信息。您应该将信息添加到意图,然后将此意图添加到 PendingIntent。
请记住在 PendingIntent 构造函数中放置一个唯一 ID,否则当您尝试获取 putExtra 值时可能会遇到一些奇怪的事情。
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(),
UUID.randomUUID().hashCode(),
aint,
PendingIntent.FLAG_UPDATE_CURRENT
);
final Intent my_intent = new Intent(this.context , Alarm_Receiver.class);
//put in extra string into my intent
//tell the clock that the user pressed the "alarm on button"
my_intent.putExtra("extra" , 1);
int state = my_intent.getExtras().getInt("extra");
Log.i("extraa" , "the state in the main activity in alarm on Button is: " + state);
我遇到了同样的问题,我发现你应该在涉及 Intent
之前放置 putExtra
在我的例子中,我缺少 PendingIntent.FLAG_UPDATE_CURRENT
作为标志。
请使用FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT
我正在通过 alarmreceiver 从服务 class 传递待处理的意图。但是,在 pendingIntent 触发后,广播接收器 class 没有接收到 intent.putExtra() 信息。这是我触发 pendingIntent
的代码Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, aint, PendingIntent.FLAG_UPDATE_CURRENT);
aint.putExtra("msg", msg);
aint.putExtra("phone", phone);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
报警接收者class在下方
public String msg, phonen;
@Override
public void onReceive(Context context, Intent intent){
Bundle extras = intent.getExtras();
msg = extras.getString("msg");
phonen = extras.getString("phone");
Log.d("onReceive", "About to execute MyTask");
Toast.makeText(context,msg, Toast.LENGTH_LONG).show();
}
从未决意图接收到的 toast 中的 msg 信息未显示。相反,显示的是空白吐司。
您必须使用 getStringExtra()
并确保字符串不为空:
Intent intent = getIntent();
msg = intent.getStringExtra("msg");
phonen = intent.getStringExtra("phone");
if(msg!=null){
Toast.makeText(context,msg,
Toast.LENGTH_LONG).show();
}
并在 PendingIntent 之前反转你的 putExtras:
Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
aint.putExtra("msg", msg);
aint.putExtra("phone", phone);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, aint, PendingIntent.FLAG_UPDATE_CURRENT);
试试这个
Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
aint.putExtra("msg", msg);
aint.putExtra("phone", phone);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(),
id,
aint,
// as stated in the comments, this flag is important!
PendingIntent.FLAG_UPDATE_CURRENT);
这是因为你是先初始化Intent然后添加到PendingIntent。之后,您将有意添加信息。您应该将信息添加到意图,然后将此意图添加到 PendingIntent。
请记住在 PendingIntent 构造函数中放置一个唯一 ID,否则当您尝试获取 putExtra 值时可能会遇到一些奇怪的事情。
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(),
UUID.randomUUID().hashCode(),
aint,
PendingIntent.FLAG_UPDATE_CURRENT
);
final Intent my_intent = new Intent(this.context , Alarm_Receiver.class);
//put in extra string into my intent
//tell the clock that the user pressed the "alarm on button"
my_intent.putExtra("extra" , 1);
int state = my_intent.getExtras().getInt("extra");
Log.i("extraa" , "the state in the main activity in alarm on Button is: " + state);
我遇到了同样的问题,我发现你应该在涉及 Intent
之前放置putExtra
在我的例子中,我缺少 PendingIntent.FLAG_UPDATE_CURRENT
作为标志。
请使用FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT