broswer 調起APP



=====================Test html ============================


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>


 <body>
  <a href="gc://com.test.dqxondgame/parameters/here">click me open local apk</a><br/>
  <!-- 對應 AndroidManifest.xml 
<data android:host="com.test.dqxondgame"
                    android:scheme="gc" />
parameters/here 可選性參數-->
  <a href="gc://www.baidu.com/parameters/here">click me open local apk</a><br/>
  <a href="https://play.google.com/store/apps/details?id=com.test.gc.webview.cmg">click me open goolge play apk</a>
123
 </body>
</html>








///////////////////////APK Android AndroidManifest.xml /////////////////////


<activity
            android:name="com.gc.activity.MainActivity"
            android:configChanges="orientation|keyboard|keyboardHidden|locale"
            android:label="@string/app_name"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity
            android:name="com.gc.activity.SonMainActivity"
            android:configChanges="orientation|keyboard|keyboardHidden|locale"
            android:label="@string/app_name"
            android:screenOrientation="landscape" >
            <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="com.test.dqxondgame"
                    android:scheme="gc" />
            </intent-filter>
        </activity>

、-----------------SonMainActivity class-----------------------


package com.gc.activity;


import android.os.Bundle;


public class SonMainActivity extends MainActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }


    
}


-----------------------傳參數sameple------------------------------------




點擊瀏覽器中的URL鏈接,啓動特定的App。
首先做成HTML的頁面,頁面內容格式如下:
<a href="[scheme]://[host]/[path]?[query]">啓動應用程序</a> 
這一句就可以了。
各個項目含義如下所示:
scheme:判別啓動的App。 ※詳細後述
host:適當記述
path:傳值時必須的key     ※沒有也可以
query:獲取值的Key和Value  ※沒有也可以
 
作爲測試好好寫了一下,如下:
<a href="myapp://jp.app/openwith?name=zhangsan&age=26">啓動應用程序</a>  
接下來是Android端。
首先在AndroidManifest.xml的MAIN Activity下追加以下內容。(啓動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:scheme="myapp" android:host="jp.app" android:pathPrefix="/openwith"/>  
</intent-filter>
HTML記述的內容加入<data …/>。
其中必須的內容僅scheme,沒有其他內容app也能啓動。
※注意事項:intent-filter的內容【android.intent.action.MAIN】和 【android.intent.category.LAUNCHER】這2個,不能與這次追加的內容混合。
                 所以,如果加入了同一個Activity,請按以下這樣做,否則會導致應用圖標在桌面消失等問題。
複製代碼
<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="myapp" android:host="jp.app" android:pathPrefix="/openwith"/>  
</intent-filter> 
複製代碼
這樣的話,沒有問題。
 
接下來在Activity中需要取值的地方添加以下代碼,我是直接寫在OnCreate函數裏的:
Intent i_getvalue = getIntent();  
String action = i_getvalue.getAction();  
  
if(Intent.ACTION_VIEW.equals(action)){  
    Uri uri = i_getvalue.getData();  
    if(uri != null){  
        String name = uri.getQueryParameter("name");  
        String age= uri.getQueryParameter("age");  
    }  
}
這樣就能獲取到URL傳遞過來的值了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章