JS喚醒Android APP(包括在外部瀏覽器和WebView)

1.AndroiManifest.xml中配置

請在App啓動的第一個Activity的那個節點中加入

<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="arseeds.com"       
                                       android:scheme="zhaojian"/>
</intent-filter>
例如我是以GuideActivity爲第一個啓動的Activity,配置如下

<activity android:name=".base.activity.GuideActivity">   
 <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="arseeds.com"         
   android:scheme="zhaojian"/> 
   </intent-filter>
</activity>
data中的host和scheme可以自定義

2.在瀏覽器中喚醒APP

需要在第一個啓動的Activity中的onCreate中加入

   Intent intent = getIntent();
          Uri uri = intent.getData();
        if (uri != null) {     
        String pid = uri.getQueryParameter("pid");  
        }
pid爲自定義鏈接的參數,如果僅僅只是喚醒APP,就可以忽略此步驟

3.在WebView中的喚醒

需要在WebView中配置

webView.setWebViewClient(new WebViewClient(){ 
            @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { 
                    Uri uri=Uri.parse(url);
                   if(uri.getScheme().equals("zhaojian")&&uri.getHost().equals("arseeds.com")){ 
                            String arg0=uri.getQueryParameter("arg0");
                           String arg1=uri.getQueryParameter("arg1"); 
                      }else{ 
          view.loadUrl(url); 
        } return true; 
}});
拿到了參數,剩下的就不用我說了

4.最重要的是在html中的配置

<html>
       <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
                      <title>Insert title here</title>
       </head> 
        <body> 
                  <a href="zhaojian://arseeds.com/?pid=1">打開app</a><br/>
           </body></html>

發佈了43 篇原創文章 · 獲贊 6 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章