与URL启动应用程序 [英] Launch app with URL
问题描述
我读过关于Android的意图,但在这里不用我的问题。我想推出一个应用程序在我的Android手机,在网络浏览器链接的点击。例: 如果该链接为mycam:// HTTP://camcorder.com,mycam://作为某种标签的推出我的应用程序,但我想传递的http://摄像机。 COM作为一个字符串在启动该应用程序。
I've read about intents in android but here goes my question. I'd like to launch an app on my android phone with the click of a link in the web browser. Example: If the link is "mycam://http://camcorder.com", "mycam://" acts as some kind of "tag" to launch my app but I'd like to pass "http://camcorder.com" as a string to that app on start.
请帮助!
谢谢!
推荐答案
有一个在浏览器应用程序的源$ C $ C的方法,:
there is a method in the Browser app source code, :
public boolean shouldOverrideUrlLoading(WebView view, String url) { ... }
在一个URL点击,它尚未开始加载:
After a url clicked and it's not yet starting to load:
-
的URL转换为意图
converts the url to intent
Intent intent;
// perform generic parsing of the URI to turn it into an Intent.
try {
intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
} catch (URISyntaxException ex) {
Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
return false;
}
如果它没有开始市场://(或predefined方案),尝试startActivityIfNeeded()
if it don't start with market:// (or some predefined schemes), try startActivityIfNeeded()
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setComponent(null);
try {
if (startActivityIfNeeded(intent, -1)) {
return true;
}
} catch (ActivityNotFoundException ex) {
// ignore the error. If no application can handle the URL,
// eg about:blank, assume the browser can handle it.
}
这是非常有用的信息!我在一个简单的code重新上场的情况:
It's very useful information! I re-play the situation in a simple code:
Intent intent = Intent.parseUri("mycam://http://camcorder.com", Intent.URI_INTENT_SCHEME);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setComponent(null);
System.out.println(intent);
结果将提供线索,我写的意图过滤器的活动:
The result will provide clues for me to write an activity with the intent-filter:
<activity android:name=".MyCamActivity" android:label="@string/app_name">
<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="mycam" />
</intent-filter>
</activity>
PS。不要忘了在 android.intent.category.DEFAUL
最后,你的活动可以通过mycam调用://方案
Finally, your Activity can invoke by mycam:// scheme
这篇关于与URL启动应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!