android scheme链接打开本地应用
一、提供scheme供别人打开自己的应用
1.1,manifest中配置
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!--todo jafir://main.app?kea1=传值成功-->
<data android:scheme="jafir" android:host="main.app">
</data>
</intent-filter>
1.2,实际应用(原生调用)
String url = "jafir://main.app"
Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(in);
如果要传递参数:后面格式为mqqwpa://im/chat?chat_type=wpa&uin=522648467
Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(in);
如果要传递参数:后面格式为mqqwpa://im/chat?chat_type=wpa&uin=522648467
1.3,获取Scheme跳转的参数
Uri uri = getIntent().getData();
if (uri != null) {
// 完整的url信息
String url = uri.toString();
Log.e(TAG, "url: " + uri);
// scheme部分
String scheme = uri.getScheme();
Log.e(TAG, "scheme: " + scheme);
// host部分
String host = uri.getHost();
Log.e(TAG, "host: " + host);
//port部分
int port = uri.getPort();
Log.e(TAG, "host: " + port);
// 访问路劲
String path = uri.getPath();
Log.e(TAG, "path: " + path);
List<String> pathSegments = uri.getPathSegments();
// Query部分
String query = uri.getQuery();
Log.e(TAG, "query: " + query);
//获取指定参数值
String goodsId = uri.getQueryParameter("goodsId");
Log.e(TAG, "goodsId: " + goodsId);
}
二、 webview中链接打开应用
网页上<a href="xl://goods:8888/goodsDetail?goodsId=10011002">打开商品详情</a>
String url = "http://wpa.qq.com/msgrd?v=3&uin=522648467&site=qq&menu=yes";
WebViewClient webViewClient = new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(final WebView view, String url) {
if (url.startsWith("http") || url.startsWith("https")) { //http和https协议开头的执行正常的流程
return false;
} else { //其他的URL则会开启一个Acitity然后去调用原生APP
Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
if (in.resolveActivity(getPackageManager()) == null) {
//说明系统中不存在这个activity
view.post(new Runnable() {
@Override
public void run() {
Toast.makeText(MainActivity.this, "应用未安装", Toast.LENGTH_SHORT).show();
view.loadUrl(failUrl);
}
});
} else {
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(in);
//如果想要加载成功跳转可以 这样
view.post(new Runnable() {
@Override
public void run() {
view.loadUrl(successUrl);
}
});
}
return true;
}
}
};
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(webViewClient);
webView.loadUrl(url);
三、如何判断一个Scheme是否有效
PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("xl://goods:8888/goodsDetail?goodsId=10011002"));
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isValid = !activities.isEmpty();
if (isValid) {
startActivity(intent);
}
四、有哪些应用的scheme URL
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("xl://goods:8888/goodsDetail?goodsId=10011002"));
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isValid = !activities.isEmpty();
if (isValid) {
startActivity(intent);
}
四、有哪些应用的scheme URL
QQ的url是 mqq://
微信是weixin://
淘宝taobao://
点评dianping://
dianping://search
微博 sinaweibo://
名片全能王camcard://
weico微博weico://
支付宝alipay://
豆瓣fm:doubanradio://
微盘 sinavdisk://
网易公开课ntesopen://
美团 imeituan://
京冬openapp.jdmoble://
人人renren://
我查查 wcc://
1号店wccbyihaodian://
有道词典yddictproapp://
知乎zhihu://
优酷 youku://
参考链接:http://www.jianshu.com/p/45af72036e58