如何通过网页打开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,配置如下:

<?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/?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

更多相关文章

  1. Android(安卓)Wifi模块学习
  2. Android在Activity中取消EditText自动获取焦点的默认行为
  3. android 截取头像
  4. Android实现图标水印
  5. Android中Intent传递对象的两种方法(Serializable,Parcelable)!
  6. 打造android ORM框架opendroid(二)——自动创建数据库
  7. android 获取uri的正确文件路径的办法
  8. android 使用log4j SLF4J 输出日志到文件中
  9. Android在子线程当中更新UI界面(TextView、ImageView)

随机推荐

  1. Android(安卓)Studio 更新时提示connecti
  2. Android(安卓)视频播放
  3. android软件音量控制
  4. [Android]Android(安卓)颜色大全 colors.
  5. Android(安卓)ImageView图片自适应
  6. Android(安卓)& Java规范
  7. Android进入商店并跳转到指定应用
  8. 从简单的android 登陆应用 ,学习布局,
  9. Android中的几种网络请求方式详解
  10. RXJava与Retrofit联合使用