【发布时间】:2015-11-14 11:20:10
【问题描述】:
我正在尝试从 Chrome 打开我的应用,但它对我不起作用..
这是官方(非常有限的)文档: Android Intents with Chrome
我发现了一堆类似的问题,但似乎没有一个解决方案对我有用 - 而且,答案有点零散,所以我正在寻找完整的答案并举例说明。一旦我们找到它,我很乐意发布我的完整解决方案:)
- Launching an Android Application from the Browser
- Open Android app from URL using intent-filter not working
这是我现在得到的:
AndroidManifest.xml:
<activity android:name=".ActMain" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
<data android:scheme="sharedr" />
<data android:host="pocketr.rejh.nl" />
<data android:pathPattern="/.*" />
</intent-filter>
</activity>
Javascript(当用户点击一个元素时它会建立意图链接):
var url = "http://icerrr.rejh.nl/"
var title = "Example"
var intenturl = "intent://sharedr/#Intent;"
+ "package=com.rejh.sharedr;"
+ "S.action=share;"
+ "S.type=url;"
+ "S.title="+ app.helpers.encodeURIComponent(title) +";"
+ "S.url="+ app.helpers.encodeURIComponent(url) +";"
+ "end";
alert(intenturl); // for debugging
try {
window.location.href=intenturl;
} catch(e) { alert(JSON.stringify(e)); }
我从这个函数中得到以下“链接”:
intent://sharedr/#Intent;package=com.rejh.sharedr;S.action=share;S.type=url;S.title=Example;S.url=http%3A%2F%2Ficerrr.rejh.nl%2F;end
人类可读:
intent://sharedr/#Intent;
package=com.rejh.sharedr;
S.action=share;
S.type=url;
S.title=Example;
S.url=http%3A%2F%2Ficerrr.rejh.nl%2F;
end
所以它的工作原理如下:它打开 Play 商店并显示“找不到项目”和一个刷新按钮。所以它打开了一个活动,但不是正确的..
我试图从中触发的网站是 http://pocketr.rejh.nl
帮助?
完整答案
正如下面的 Mattia 所指出的(我自己只是想出了这个问题,但发现答案在等着我……:P)
首先:我忘记在我的意图链接中包含“scheme=sharedr”:
intent://sharedr/#Intent;
package=com.rejh.sharedr;
S.action=share;
S.type=url;
S.title=Example;
S.url=http%3A%2F%2Ficerrr.rejh.nl%2F;
end
应该是(注意第二行):
intent://sharedr/#Intent;
scheme=sharedr;
package=com.rejh.sharedr;
S.action=share;
S.type=url;
S.title=Example;
S.url=http%3A%2F%2Ficerrr.rejh.nl%2F;
end
然后,在 android 清单中:
<activity android:name=".ActMain" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="sharedr" android:host="sharedr" android:path="/"/>
</intent-filter>
</activity>
其中 'android:scheme="sharedr"' 是 'scheme=sharedr' 部分,而 'android:host="sharedr"' 对应于 'intent://sharedr/'
谢谢!
【问题讨论】:
标签: android google-chrome android-intent