我有一个BroadcastReceiver,它通过Intent从Activity获取数据, 与PendingIntent。
发送数据:
Intent intent = new Intent(addOne.this,AlarmReceiver.class);
intent.putExtra("msg", title.getText().toString());
intent.putExtra("note", note.getText().toString());
int id =...;
PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
获取数据:
@Override
public void onReceive(Context context2, Intent intent) {
String msg=intent.getStringExtra("msg");
String title=intent.getStringExtra("msg");
...
}
当调用onReceive时,我得到一个NullPointerException。
我的BroadcastReceiver-manifest:
<receiver android:name=".AlarmReceiver" android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
答案 0 :(得分:0)
将您的待处理意图设置为如下所示,然后尝试
PendingIntent alarmIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_CANCEL_CURRENT);