简体   繁体   English

Android PhoneGap意图过滤,浏览器回调正在运行的应用程序?

[英]Android PhoneGap intent-filter, Browser calling back to running app?

I have a Cordova/PhoneGap 2.0 based Android application, and I'm attemping to use intent-filters to register a custom protocol handler. 我有一个基于Cordova / PhoneGap 2.0的Android应用程序,我正在尝试使用intent-filters来注册自定义协议处理程序。

So my PhoneGap app launches, does its thing, and then later calls out to the Browser app to a remote web page (via navigator.app.loadUrl('http://example.com/') or window.open('http://example.com/')) . 所以我的PhoneGap应用程序启动,完成它的工作,然后调用浏览器应用程序到远程网页(通过navigator.app.loadUrl('http://example.com/')window.open('http://example.com/')) A link on that remote web page in the Browser ( myprotocol://do/stuff ) will bring my app back to the foreground via the intent-filter for the data scheme. 浏览器中的远程网页上的链接( myprotocol://do/stuff )将通过数据方案的intent-filter将我的应用程序带回到前台。 And via the WebIntents plugin I can get and parse this url in my app. 通过WebIntents插件,我可以在我的应用程序中获取并解析此URL。

This is working in Android 4.0.4 on an HTC One X (and thus a 'Sense' version of Android). 这适用于HTC One X上的Android 4.0.4(因此也是Android的'Sense'版本)。

The same code does not work on Android 4.0.4 or 4.1.1 on a Galaxy Nexus, or 4.0.1 on a Nexus S. 相同的代码不适用于Galaxy Nexus上的Android 4.0.4或4.1.1,或Nexus S上的4.0.1。

The odd issue I see on "stock" Android is that while the intent-filter is triggered, it launches a new view of the app, and IN the Android Browser. 我在“股票”Android上看到的奇怪问题是,当触发了intent-filter时,它会启动应用程序的新视图,并 Android浏览器中启动。 I can see it reloads the html page. 我可以看到它重新加载html页面。 It does not bring the existing app instance to the foreground as expected. 它没有按预期将现有的应用程序实例带到前台。 Looking at logcat I see that this is actually the same I/Web Console process, but it's loaded the html again. 看看logcat,我看到这实际上是同一个I / Web Console进程,但是它再次加载了html。 There is no new log of 'Start proc'. 没有新的“Start proc”日志。

And oddly I now have two instances of my app displayed in the task manager. 奇怪的是,我现在在任务管理器中显示了我的应用程序的两个实例。 The original app instance is still there with my custom app icon. 原始应用实例仍然存在我的自定义应用图标。 And the Browser process has its own Browser app icon, but is clearly running my app UI. 浏览器进程有自己的浏览器应用程序图标,但显然正在运行我的应用程序UI。 I can switch and interact with each webview individually. 我可以单独切换和与每个webview交互。 It's almost like my process now has two web views - one in the app and a second web view now running in the Browser process. 这几乎就像我的进程现在有两个Web视图 - 一个在应用程序中,另一个Web视图现在在浏览器进程中运行。

I currently have something like this in AndroidManifest.xml: 我目前在AndroidManifest.xml中有这样的东西:

<activity android:name="MyApp" android:label="@string/app_name" 
          android:screenOrientation="portrait"
          android:launchMode="singleTop"
          android:configChanges="orientation|keyboardHidden">

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

    <intent-filter>
      <data android:scheme="myprotocol" />
      <action android:name="android.intent.action.VIEW" />
      <category android:name="android.intent.category.BROWSABLE" />
      <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

</activity>

My App is using the WebIntents plugin and the NFC plugin, but I also see the same issue without them. 我的应用程序正在使用WebIntents插件和NFC插件,但如果没有它们,我也会看到同样的问题。 I have also reproduced this issue in PhoneGap 1.8.1 and 1.9.0 as well. 我也在PhoneGap 1.8.1和1.9.0中重现了这个问题。

I can also see via logging that for the HTC, DroidGap.onCreate() does not get called when the intent is triggered, but on the Nexus it does. 我还可以通过日志记录看到,对于HTC, DroidGap.onCreate()在触发意图时不会被调用,但是在Nexus上它会被调用。

Am I doing something incorrect in hooking up the intent-filter this way? 我是否通过这种方式连接了意图过滤器?

How can I trigger the existing app instance to be called, instead of a new one in the browser, on stock Android 4.x? 如何在Android 4.x股票上触发现有的应用实例,而不是浏览器中的新应用实例?

Using android:launchMode="singleTask" rather than android:launchMode="singleTop" for the activity solved this for my scenario. 使用android:launchMode="singleTask"而不是android:launchMode="singleTop"为活动解决了这个问题。

It appears that HTC simply handles this differently than default Android should. 似乎HTC只是处理这个与默认Android应该不同的方式。

Reading http://developer.android.com/guide/components/tasks-and-back-stack.html and viewing how, for example the Google Play Store App is launched by the Browser ( in the Browser, in the Task list) on stock Android indicates the behavior I saw was intended, if a bit confusing. 阅读http://developer.android.com/guide/components/tasks-and-back-stack.html并查看如何通过浏览器启动Google Play商店应用程序( 浏览器中,在任务列表中)现货Android表明我看到的行为是有意的,如果有点混乱。

I only have a single activity and intend for there to only be a single instance of my app, so "singleTask" here solved the issue. 我只有一个活动,并打算只有我的应用程序的单个实例,所以“singleTask”在这里解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM