如何通過網頁開啟Android APP

來源:互聯網
上載者:User

標籤:

如何通過網頁開啟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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.