我读过有关 android 中的意图的内容,但这是我的问题。我想通过点击网络浏览器中的链接来启动我的 Android 手机上的应用程序。例子:
如果链接是“mycam://http://camcorder.com http://camcorder.com”,“mycam://”充当某种“标签”来启动我的应用程序,但我想传递“http://camcorder.com http://camcorder.com" 在启动时作为该应用程序的字符串。
请帮助!
Thanks!
浏览器应用程序源代码中有一个方法:
public boolean shouldOverrideUrlLoading(WebView view, String url) { ... }
单击网址后尚未开始加载:
-
将 url 转换为意图
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;
}
-
如果它不以 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.
}
这是非常有用的信息!我用一些简单的代码重演了这种情况:
Intent intent = Intent.parseUri("mycam://http://camcorder.com", Intent.URI_INTENT_SCHEME);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setComponent(null);
System.out.println(intent);
结果将为我使用意图过滤器编写活动提供线索:
<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>
附言。别忘了android.intent.category.DEFAULT
.
最后,您的 Activity 可以通过以下方式调用mycam://yourscheme
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)