Car*_*tes 6 android android-intent
我已经阅读了关于Android中的意图,但这里是我的问题.我想通过点击网络浏览器中的链接在我的Android手机上启动应用程序.示例:如果链接是"mycam:// http://camcorder.com ","mycam://"充当某种"标记"来启动我的应用程序,但我想通过" http:// camcorder.com "作为开始时该应用程序的字符串.
请帮忙!
谢谢!
浏览器应用源代码中有一个方法,:
public boolean shouldOverrideUrlLoading(WebView view, String url) { ... }
Run Code Online (Sandbox Code Playgroud)
点击一个网址后,它还没有开始加载:
将网址转换为意图
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;
}
Run Code Online (Sandbox Code Playgroud)如果它不是以market://(或某些预定义的方案)开头,请尝试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.
}
Run Code Online (Sandbox Code Playgroud)这是非常有用的信息!我用一个简单的代码重新演绎这种情况:
Intent intent = Intent.parseUri("mycam://http://camcorder.com", Intent.URI_INTENT_SCHEME);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setComponent(null);
System.out.println(intent);
Run Code Online (Sandbox Code Playgroud)
结果将为我提供使用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>
Run Code Online (Sandbox Code Playgroud)
PS.不要忘记android.intent.category.DEFAUL.
最后,您的Activity可以通过mycam:// scheme调用
| 归档时间: |
|
| 查看次数: |
8479 次 |
| 最近记录: |