这是我创建 PendingIntent 的代码:
Bundle b = new Bundle();
b.putCharSequence("Dest", myTrip.inboundFlights.get(i).getDestination());
b.putCharSequence("Orig", myTrip.inboundFlights.get(i).getOrigin());
b.putLong("DepTime", myTrip.inboundFlights.get(i).getEtd().getTime());
Intent myIntent = new Intent("NotificationActivity");
myIntent.putExtras(b);
System.out.println(myIntent.getExtras());
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
根据我发现的所有内容,这应该可以工作,但是当我尝试访问活动中的捆绑包时,它是 NULL。
这是活动:
public class NotificationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
System.out.println(savedInstanceState);
NotificationManager mNotificationManager;
mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Context context = getApplicationContext();
int icon = R.drawable.app_icon;
CharSequence tickerText = "tickerText";
CharSequence contentTitle = "title";
CharSequence contentText = savedInstanceState.getCharSequence("Dest"); //Here's the crash with null pointer exception
Intent notificationIntent = new Intent(this, NotificationActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
long when = savedInstanceState.getLong("DepTime");
int id = (int) savedInstanceState.getLong("DepTime");//temporary will change later
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify( id, notification);
finish();
}
}
这是 AndroidManifest.xml 中用于活动的部分:
<activity android:name=".Helpers.NotificationActivity" >
<intent-filter>
<action android:name="NotificationActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
我错过了什么?