如何设置基于Android的PhoneGap的应用程序的初始页面的网址? [英] How to set the initial page url in android phonegap-based application?

查看:154
本文介绍了如何设置基于Android的PhoneGap的应用程序的初始页面的网址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个基于PhoneGap的,Android应用程序,我正在写code处理通知。这里是一块code的这是应该提高的通知的一个片段:

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);

我要prepare的意图,因此在打开相关的通知是关于什么的网页应用程序。到目前为止,我已经找到与此相关的唯一事情是以下行,放在 MainActivity 类(扩展一个 DroidGap 班),在的onCreate 方法:

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);

但我想从code动态设置URL上面,或者,在最坏(或最好的,我真的不知道......)的情况下,参数化,并传递参数从onCreate方法code以上。我怎么做?在此先感谢...

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...

推荐答案

您可以通过URL作为一个额外的对你的意图:

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);

然后在你的活动重载<一个href=\"http://developer.android.com/reference/android/app/Activity.html#onNewIntent%28android.content.Intent%29\"相对=nofollow> onNewIntent mehotd接受它,并调用super.loadUrl。如果您注册为您的活动 SINGLE_TOP 像你这样,该方法 onNewInent(意向)将如果活动被称为已创建。

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");    
}

修改

您应该叫 onNewIntent 从您的onCreate activitiy:

You should call onNewIntent from your onCreate activitiy:

  public void onCreate(){
     super.onCreate(savedInstanceState);
      onNewIntent(getIntent());          
}

您有三种可能的情况:

1 - 您的活动没有运行。在应用程序图标的用户点击,开始您的应用程序。 的onCreate()运行正常, getIntent()将返回用于创建您的应用程序的意图。没有网​​址以实现这一意图额外定义,所以你会回落到默认页。

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 - 您的活动没有运行。它跑过去,通知被提出,但不得以任何理由的活动中丧生。该用户在栏中的通知。于是,他点击了通知,并开始您的活动。 的onCreate()运行正常,但这个时候你有一个网​​址'在你的意图额外的,所以 onNewIntent`将打开你定义为额外对你的意图的URL。

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你的活动正在运行。它被创造了,现在它仍然存在,但是它在后台暂停。用户点击您的通知,并带来了它。 的onCreate 不叫,因为你的行为已经存在。但 onNewIntent 是。意图您定义。因此,有一个网​​址额外的,而URL被装载在 onNewIntent

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.

<一个href=\"http://stackoverflow.com/questions/1198558/how-to-send-parameters-from-a-notification-click-to-an-activity\">Refer对这个问题了解更多详情!

这篇关于如何设置基于Android的PhoneGap的应用程序的初始页面的网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆