【问题标题】:How to set the initial page url in android phonegap-based application?如何在基于 android phonegap 的应用程序中设置初始页面 url?
【发布时间】:2013-05-24 18:50:49
【问题描述】:

我正在开发一个基于 phonegap 的 Android 应用程序,并且我正在编写代码来处理通知。这是应该引发通知的一段代码的 sn-p:

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

我想准备 Intent,以便它在与通知内容相关的页面中打开应用程序。到目前为止,我发现的唯一与此相关的是以下行,位于MainActivity 类(扩展DroidGap 类)中的onCreate 方法中:

super.loadUrl("file:///android_asset/www/index.html", 10000);

但我想从上面的代码中动态设置该 URL,或者,在最坏的(或最好的,我真的不知道......)的情况下,将其参数化并将参数从上面的代码传递给 onCreate 方法。我怎么做?提前谢谢...

【问题讨论】:

    标签: android cordova startup


    【解决方案1】:

    您可以将网址作为附加信息传递给您的意图:

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

    然后在您的活动上重载onNewIntent mehotd 以接收它,并调用 super.loadUrl。如果您像以前一样将您的活动注册为SINGLE_TOP,则如果活动已创建,则将调用方法onNewInent(intent)

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

    编辑

    你应该从你的 onCreate 活动中调用onNewIntent

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

    你有三种可能的情况:

    1- 您的活动未运行。用户点击应用图标,启动你的应用。 onCreate() 按预期运行,getIntent() 将返回用于创建应用程序的意图。该意图中没有额外定义url,因此您将退回到默认页面。

    2- 您的活动未运行。它在过去运行,发出了通知,但由于任何原因活动被终止。用户在栏中有通知。因此,他单击通知并开始您的活动。 onCreate() 按预期运行,但这次你确实有一个 url' extra in your intent, soonNewIntent` 将打开你定义为你意图的额外 URL。

    3- 您的活动正在运行。它已创建,现在它仍然存在,但它在后台暂停。用户单击您的通知并将其显示出来。 onCreate 未被调用,因为您的活动已存在。但onNewIntent 是。根据您定义的意图。所以有一个额外的url,并且那个url被加载到onNewIntent

    Refer to this question for more details!

    【讨论】:

    • 感谢您的回答!我不清楚在我的onCreate() 方法中我必须做什么。我可以在onCreate() 方法中访问当前的Intent(并检查额外内容)吗?我怎么做?再次感谢!
    • 感谢您的完整回答!还有一个问题,只是为了确定:如果应用程序在后台,并且在没有任何通知的情况下被用户唤醒,则调用onNewIntent(),不是吗?那么它会退回到默认页面吗?我知道这可能很明显,但我是 android 开发新手,我需要确定...
    • 我不确定答案,但我的第一个猜测是肯定的。它将显示之前显示的任何内容。一个非常简单的测试方法是按下主页键,然后再次打开应用程序。请在此处发布您的发现!
    • super.loadUrl(extras.getString(url)); 应该是super.loadUrl(extras.getString("url"));
    猜你喜欢
    • 2011-02-10
    • 2011-02-14
    • 2017-10-30
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多