如何通过网页打开Android APP

如何通过网页打开Android APP

1、首先在编写一个简单的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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值