Reputation: 4595
I use an AlarmManager to start an activity via a PendingIntent.
Intent smon = new Intent(ctxt, VideoActivty.class);
final Bundle extra = new Bundle();
extra.putString("extrastring","monday");
smon.putExtras(extra);
//smon.putExtra("extrastring","monday");
smon.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent psmon = PendingIntent.getActivity(ctxt, 0, smon, 0);
Calendar calSet1 = Calendar.getInstance();
calSet1.set(Calendar.MONTH, c.get(Calendar.MONTH));
calSet1.set(Calendar.YEAR, c.get(Calendar.YEAR));
calSet1.set(Calendar.DAY_OF_WEEK, 2);
calSet1.set(Calendar.HOUR_OF_DAY, hsmon);
calSet1.set(Calendar.MINUTE, msmon);
calSet1.set(Calendar.SECOND, 0);
calSet1.set(Calendar.MILLISECOND, 0);
int delay1=0;
if(today.get(Calendar.DAY_OF_WEEK)>2)delay1=7 * 24 * 60 * 60 * 1000;
if((today.get(Calendar.DAY_OF_WEEK)==2)&&(today.get(Calendar.HOUR_OF_DAY)>hsmon))delay1=7 * 24 * 60 * 60 * 1000;
if((today.get(Calendar.DAY_OF_WEEK)==2)&&(today.get(Calendar.HOUR_OF_DAY)==hsmon)&&(today.get(Calendar.MINUTE)>msmon))delay1=7 * 24 * 60 * 60 * 1000;
Log.e("delay 1",Integer.toString(delay1));
//calSet.setTimeZone(TimeZone.getTimeZone("UTC"));
mgr.setRepeating(AlarmManager.RTC_WAKEUP, calSet1.getTimeInMillis()+delay1,
7 * 24 * 60 * 60 * 1000, psmon);
As you can see there is an Extra in my Intent: "extrastring".
Now, when I try tor read that Extra from the started Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//
final Bundle extra = getIntent().getExtras();
String message="cazzo";
if (extra != null) {
message=extra.getString("extrastring");
}
Log.e("intent che è partito",message);
I get a NullPointerException.
Thanks for any help
07-19 13:34:18.336: E/AndroidRuntime(14109): FATAL EXCEPTION: main
07-19 13:34:18.336: E/AndroidRuntime(14109): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.videdrome/com.example.videdrome.VideoActivty}: java.lang.NullPointerException: println needs a message
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.os.Handler.dispatchMessage(Handler.java:99)
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.os.Looper.loop(Looper.java:137)
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-19 13:34:18.336: E/AndroidRuntime(14109): at java.lang.reflect.Method.invokeNative(Native Method)
07-19 13:34:18.336: E/AndroidRuntime(14109): at java.lang.reflect.Method.invoke(Method.java:511)
07-19 13:34:18.336: E/AndroidRuntime(14109): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-19 13:34:18.336: E/AndroidRuntime(14109): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-19 13:34:18.336: E/AndroidRuntime(14109): at dalvik.system.NativeStart.main(Native Method)
07-19 13:34:18.336: E/AndroidRuntime(14109): Caused by: java.lang.NullPointerException: println needs a message
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.util.Log.println_native(Native Method)
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.util.Log.e(Log.java:231)
07-19 13:34:18.336: E/AndroidRuntime(14109): at com.example.videdrome.VideoActivty.onCreate(VideoActivty.java:41)
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.app.Activity.performCreate(Activity.java:5104)
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-19 13:34:18.336: E/AndroidRuntime(14109): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-19 13:34:18.336: E/AndroidRuntime(14109): ... 11 more
Upvotes: 3
Views: 2216
Reputation: 4787
I have been doing the similar thing using Bundle
final Intent notificationIntent = new Intent(context, A.class);
notificationIntent.putExtras(extra);
final PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
where extra is a Bundle :
final Bundle extra = new Bundle();
extra.putString("Message", "hello");
You can retrieve the value of Bundle in onCreate of started Activity:
final Bundle extra = getIntent().getExtras();
if (extra != null) {
String message=extra.getString("Message");
}
Moreover , you can use flag PendingIntent.FLAG_UPDATE_CURRENT
This flag works like this:
if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent. This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.
Upvotes: 4
Reputation: 4351
From your description, i suspect that the NullPointerException
is caused because the getIntent()
methiod returns null
. There may be two possible reasons for this to happen:
getIntent()
before the called Activity's onCreate()
method is fired.Hope that helps!
Upvotes: 1
Reputation: 4001
Try this way
String result = getIntent().getExtras().getString("extrastring");
Upvotes: 1
Reputation: 9092
try this:
Bundle extras = getIntent().getExtras();
String message = extras.getString("extrastring");
Upvotes: 1
Reputation: 4296
What you probably want to do is put that string into a Bundle
, and add that to your Intent (using putExtras().
Then you can call getExtras() on your intent to get your Bundle back, from which you can get your string back.
Upvotes: 1