首页 > 代码库 > Android通过网页启动APP

Android通过网页启动APP

http://www.cnblogs.com/yejiurui/p/3413796.html

1、测试网页

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>TestStartApp</title>
    </head>
    <body>
        <a href=http://www.mamicode.com/"m://test.com/?package=com.tencent.mobileqq&arg1=1">打开QQ
>

2、需要对WebView设置如下:

	private WebView mWebView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_main);

//		if (savedInstanceState == null) {
//			getSupportFragmentManager().beginTransaction()
//					.add(R.id.container, new PlaceholderFragment()).commit();
//		}
		mWebView = (WebView) findViewById(R.id.webview);
		mWebView.setWebViewClient(new WebViewClient(){

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				Log.e("Test", url);
				Uri uri=Uri.parse(url);
				if(uri.getScheme().equals("m")&&uri.getHost().equals("test.com")){
					String pkgName=uri.getQueryParameter("package");
					String arg1=uri.getQueryParameter("arg1");
					Log.e("Test", "package = "+pkgName+", arg1 = "+arg1);
					Intent intent =null;
					intent = getPackageManager().getLaunchIntentForPackage(pkgName);
					if(intent != null){
						startActivity(intent);
					}
					return true;
				}else
					return super.shouldOverrideUrlLoading(view, url);
			}
			
		});
		mWebView.loadUrl("http://***************/testApp.html");
	}

点击打开QQ,如果你手机上安装了QQ,就可以启动了。

Android通过网页启动APP