Arn*_*nab 25 android android-intent
我通过alarmreceiver从服务类传递一个待处理的意图.但是,在pendingIntent触发后,broadcastreceiver类没有接收到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);
Run Code Online (Sandbox Code Playgroud)
警报接收器类如下
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();
}
Run Code Online (Sandbox Code Playgroud)
未显示正在从待处理意图接收的Toast中的msg信息.而是显示空白吐司.
aye*_*don 45
试试这个
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);
Run Code Online (Sandbox Code Playgroud)
小智 5
请记住在PendingIntent构造函数中放置一个唯一的ID,否则当您尝试获取putExtra值时,您可能会得到一些奇怪的信息。
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(),
UUID.randomUUID().hashCode(),
aint,
PendingIntent.FLAG_UPDATE_CURRENT
);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
13579 次 |
最近记录: |