標籤:
如何通過網頁開啟Android APP1、首先在編寫一個簡單的html頁面
html頁面中只有一個簡單的串連,代碼如下:
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <a href="ky://www.keyisoftware.com/vpn?name=daqin&pwd=12345678">開啟app</a><br/> </body></html>
2、設定APP的AndroidManifest.xml檔案
AndroidManifest.xml中需要過濾Intent,配置如下:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="test.keyisoftware.tstartappbyweb" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="14" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <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="www.keyisoftware.com" android:scheme="ky" /> </intent-filter> </activity> </application></manifest>
注意:android:scheme中的名稱必須是全部小寫。
3、網頁傳遞資料到APP中
只是開啟APP是最為基本的功能,實際的應用中,通常是在開啟的時候同時傳遞相應的參數進去,如實現單點登入,二維碼掃描等。
如上面的串連中,傳遞了對應的使用者名稱和密碼:
ky://www.keyisoftware.com/vpn?name=daqin&pwd=12345678
而此時,需要在APP中擷取對應的資訊,實現代碼如下:
package test.keyisoftware.tstartappbyweb;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.util.Log;import android.widget.Toast;public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 擷取網頁傳遞過來的資料 Intent intent = getIntent(); String scheme = intent.getScheme(); Uri uri = intent.getData(); StringBuffer buffer = new StringBuffer(); if (uri != null) { // 擷取網域名稱,不包含連接埠 buffer.append("Host=" + uri.getHost()); // 擷取傳入的全部的字串 buffer.append("DataString=" + intent.getDataString()); // 擷取路徑 buffer.append("Path=" + uri.getPath()); // 擷取全部的參數 buffer.append("Query=" + uri.getQuery()); // 擷取參數的值 buffer.append("Name=" + uri.getQueryParameter("name") + " Pwd=" + uri.getQueryParameter("pwd")); } String str = "Scheme=" + scheme + buffer.toString(); Log.i("TStartAppByWeb", str); Toast.makeText(this, str, Toast.LENGTH_SHORT).show(); }}
註:使用上述方式開啟APP的瀏覽器,核心必須是Webkit。
參考文獻:
http://www.cnblogs.com/yejiurui/p/3413796.html
http://blog.csdn.net/jdsjlzx/article/details/37700791
如何通過網頁開啟Android APP