Android网页打开指定App

一、只打开APP操作

通过用手机的浏览器(内置,第三方都可)访问一个网页,实现点击一个链接启动自己的应用,并传递数据。

首先在Mainifest文件里面对要启动的Activity添加一个过滤器。

网页需要的内容

 <html>  
  
    <head>  
  
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
      
        <title>打开App</title>  
  
    </head>  
  
    <body>  
  
        <a href="m://baoming/">打开app</a><br/>  
  
    </body>  
  
</html> 

主要的就是a标签里边的内容,href连接的地址要在Android  AndroidManifest.xml中配置

AndroidManifest.xml中的内容

第一种:(可以通过地址打开,有一个问题,运行之后可以打开app但是没有启动图标)

 <activity  
           android:name=".MainActivity"  
           android:configChanges="orientation|locale"  
           android:screenOrientation="portrait">  
           <intent-filter>  
               <action android:name="android.intent.action.MAIN" />  
  
               <category android:name="android.intent.category.LAUNCHER" />  
  
               <action android:name="android.intent.action.VIEW" />  
  
               <category android:name="android.intent.category.DEFAULT" />  
               <category android:name="android.intent.category.BROWSABLE" />  
  
               <data  
                   android:host="baoming"  
                   android:scheme="m" />  
           </intent-filter>  
       </activity>

第二种(可以启动,有启动图标)

 <activity  
           android:name=".MainActivity"  
           android:configChanges="orientation|locale"  
           android:screenOrientation="portrait">  
           <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:host="baoming"  
                   android:scheme="m" />  
           </intent-filter>  
       </activity>

二、打开APP并且获取携带数据

我们可以使用上述的方法,把一些数据传给本地app,那么首先我们更改一下网页,代码修改后:

首先修改网页内容

 

<html>
    <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="m://my.com/?arg0=marc&arg1=xie">打开app</a><br/> </body> </html>

 

(1).假如你是通过浏览器打开这个网页的,那么获取数据的方式为:

 

Uri uri = getIntent().getData();  String test1= uri.getQueryParameter("arg0");  String test2= uri.getQueryParameter("arg1");
[java]  view plain  copy
  1. Uri uri = getIntent().getData();  
  2.      if (uri != null) {  
  3.          String test1 = uri.getQueryParameter("arg0");  
  4.          String test2 = uri.getQueryParameter("arg1");  
  5.          tv.setText(test1 + test2);  
  6.      }  

 

(2)如果使用webview访问该网页,获取数据的操作为:

 
  1. webView.setWebViewClient(new WebViewClient(){  
  2.   @Override  
  3.   public boolean shouldOverrideUrlLoading(WebView view, String url) {  
  4.       Uri uri=Uri.parse(url);  
  5.           if(uri.getScheme().equals("m")&&uri.getHost().equals("my.com")){  
  6.               String arg0=uri.getQueryParameter("arg0");  
  7.               String arg1=uri.getQueryParameter("arg1");  
  8.                
  9.           }else{  
  10.               view.loadUrl(url);  
  11.           }  
  12.       return true;  
  13.   }  
  14. });  

上面只是实现了 传参等操作。但是如果需要判断是否安装了该应用。

 

[html]  view plain  cop
  1. <html>  
  2.   
  3.     <head>  
  4.   
  5.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  6.       
  7.         <title>Insert title here</title>  
  8.   
  9.     </head>  
  10.   
  11.     <body>  
  12.           <script language="javascript">  
  13.                 function openApp(){  
  14.                       
  15.                               
  16.                             window.location.href = 'm://marc.com/?arg0=marc&arg1=xie';  //app内部  
  17.                             setTimeout(function(){  
  18.                                     window.location.href='http://www.wln100.com/Aat/App/index.html';//下载app的网页  
  19.                             },500);  
  20.                        
  21.                 }  
  22.          </script>  
  23.   
  24.         <a href="javascript:openApp()">打开app</a><br/>  
  25.   
  26.     </body>  
  27.   
  28. </html>  

猜你喜欢

转载自www.cnblogs.com/dingxiansen/p/9257551.html