我尝试将PendingIntent中的值发送到Broadcast,但我不能。这是我的代码:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, alarmTimePicker.getCurrentHour());
calendar.set(Calendar.MINUTE, alarmTimePicker.getCurrentMinute());
calendar.set(Calendar.DATE, alarmDatePicker.getDayOfMonth());
Intent myIntent = new Intent(Activity_Main.this, SampleBootReceiver.class);
myIntent.putExtra("ID", 1234);
pendingIntent = PendingIntent.getBroadcast(Activity_Main.this,0, myIntent, 0);
alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 20000, pendingIntent);
这是我的BroadCast:
public class SampleBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int i= intent.getExtras().getInt("ID");
Log.w("Sample",""+ i);
Intent a =new Intent(context,RSSPullService.class);
a.putExtra("ID",i);
context.startService(a);
}
}
我将i
的值设为0
。我不明白为什么。非常感谢你。
答案 0 :(得分:1)
因为Intent中唯一改变的是附加内容。因此,PendingIntent.getActivity(...)工厂方法只是将旧的intent重新用作优化。
更改此行,
pendingIntent = PendingIntent.getBroadcast(Activity_Main.this,0, myIntent, 0);
要,
pendingIntent = PendingIntent.getBroadcast(Activity_Main.this,0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
请参阅FLAG_UPDATE_CURRENT参考。