如何通过网页打开Android APP

如何通过网页打开Android APP

1、首先在编写一个简单的html页面

html页面中只有一个简单的连接,代码如下:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title heretitle>
    head>
    <body>
        <a href="ky://www.keyisoftware.com/?name=daqin&pwd=12345678">打开appa><br/>
    body>

html>

2、设置APP的AndroidManifest.xml文件

AndroidManifest.xml中需要过滤Intent,配置如下:


<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/?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,android,app)