在网页中打开指定的应用APP

实现在网页中直接打开手机中安装的自己的应用的功能

一、实现的功能

   在网页中点击某个链接直接跳转到本地APP的功能,可以跳转到指定的Activity,同时可以向其传递参数。

二、HTML代码

代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
 </head>
 <body>
  <h1>This is a TestHTML</h1>
  <a href="test-scheme://testhost/openapp?name=user&age=20">open myapp</a>
 </body>
</html>

具体的解释:
主要的代码格式是这个链接中的内容
test-scheme://testhost/openapp?name=user&age=20
格式:[scheme]://[host]/[path]?[query]
scheme:判别启动的App
host:适当记述
path:传值时必须的key,不传值时可以没有
query:获取值的Key和Value,可以没有
注意:上边的这些内容必须和在AndroidManifest.xml中声明的intent-filter中的内容保持一致,否则无法找到并打开对应应用的Activity。

三、Android端代码

AndroidManifest.xml文件要打开的Activity中配置intent-filter,示例代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="hjking.cn.testopenmyappbyurl">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <!--<intent-filter>-->
                <!--<data-->
                    <!--android:host="testhost"-->
                    <!--android:pathPrefix="/openapp"-->
                    <!--android:scheme="test-scheme" />-->
                <!--<action android:name="android.intent.action.VIEW" />-->

                <!--<category android:name="android.intent.category.DEFAULT" />-->
                <!--<category android:name="android.intent.category.BROWSABLE" />-->
            <!--</intent-filter>-->
        </activity>
        <activity android:name=".TestActivity">
            <intent-filter>
                <data
                    android:host="testhost"
                    android:pathPrefix="/openapp"
                    android:scheme="test-scheme" />
                <action android:name="android.intent.action.VIEW" />

                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
            </intent-filter>
        </activity>
    </application>

</manifest>

上边的配置是通过点击链接打开TestActivity的,因此在TestActivity中配置,当然,这个可以在任何Activity中配置,在哪个Activity中配置,点击链接后就会跳转到哪个Activity,但是我们按返回键时会关闭APP,如果想要实现按返回键回到首页的功能,我们可以点击链接打开首页,然后再通过首页直接跳转到要打开的Activity即可。
注意:这些在Activity中配置的intent-filter必须和链接中的保持一致。
获取点击链接传递过来参数的功能,我们需要在要跳转的Activity中的onCreate方法中获取对应的参数值,代码如下:
package hjking.cn.testopenmyappbyurl;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

public class TestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        getIntentData();
    }

    private void getIntentData() {
        Intent intent = getIntent();
        String action = intent.getAction();

        if(Intent.ACTION_VIEW.equals(action)){
            Uri uri = intent.getData();
            if(uri != null){
                String name = uri.getQueryParameter("name");
                String age= uri.getQueryParameter("age");
                Toast.makeText(TestActivity.this,
                        "获取到的数据::" + "name:" + name + "age:" + age,Toast.LENGTH_LONG)
                        .show();
            }
        }
    }
}

有需要源码的可以点击下边的链接免费下载查看:
点击下载源码
参考链接:
http://blog.youkuaiyun.com/hundsong/article/details/6623500
http://m.blog.youkuaiyun.com/article/details?id=51143892
http://blog.youkuaiyun.com/jiangwei0910410003/article/details/23940445
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值