问题
I'm developing an phonegap-based android app, and I'm writing code to handle notifications. Here is a snippet of the piece of code which is supposed to raise the notification:
Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
I want to prepare the Intent so it opens the app in the page related to what the notification is about. So far, the only thing I have found related to this is the following line, placed in the MainActivity class (the one which extends DroidGap class), in the onCreate method:
super.loadUrl("file:///android_asset/www/index.html", 10000);
but I would like to set that URL dynamically from the code above, or, in the worst (or best, I really dont know...) case, parametrize it and pass parameters to the onCreate method from the code above. How do I do that? Thanks in advance...
回答1:
You can pass the url as an extra on your intent:
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.putExtra("url", "file:///android_asset/www/page_you_want_to_load.html")
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
Then on your activity overload the onNewIntent mehotd to receive it, and call super.loadUrl. If you register your activity as SINGLE_TOP like you did, the method onNewInent(intent) will be called if the activity was already created.
public void onNewIntent(Intent intent){
Bundle extras = intent.getExtras();
if(extras != null){
if (extras.containsKey("url"))
//load the url in the parameter
super.loadUrl(extras.getString(url));
}
else
//fall back in case nothing is given, load the default url.
super.loadUrl("file:///android_asset/www/index.html");
}
EDIT
You should call onNewIntent from your onCreate activitiy:
public void onCreate(){
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
}
You have three possible cases:
1- Your activity is not running. The user clicks on the app Icon, starts your app. onCreate() runs as expected, getIntent() will return the intent that was used to create your app. There is no url extra defined in that intent, so you'll fall back to the default page.
2- Your activity is not running. It ran in the past, the notification was raised but for any reason the activity was killed. The user has the notification in the bar. So he clicks on the notification and starts your activity. onCreate() runs as expected, but this time you DO have a url' extra in your intent, soonNewIntent` will open the url you defined as extra on your intent.
3- Your activity is running. It was created, now it still exists but it's on the background paused. The user clicks on your notification and brings it up. onCreate is not called because your activity already exists. but onNewIntent is. With the intent you defined. So there is a url extra, and that url is loaded on onNewIntent.
Refer to this question for more details!
来源:https://stackoverflow.com/questions/16730695/how-to-set-the-initial-page-url-in-android-phonegap-based-application