网页调起安卓APP的具体方式

前言

这种功能在很多大型App上都有出现,像京东、QQ、淘宝都很容易发现。外部浏览器调起App,App跳转外部浏览器网页。不得不说,这样的操作就有很强的产品塑造性了。

具体实现

  • App跳转外部浏览器网页

    这个实现很简单,两三行代码的事情,不复杂!

    1.获取需要打开的uri

    2.通过隐性intent唤起浏览器

    Uri uri = Uri.parse("https://www.baidu.com");
    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    startActivity(intent);
    
  • 外部浏览器调起App

    这个实现要先来说说URI原理性知识。Android上的URI主要有三部分组成,scheme, authoritypath。其中authority又分为hostport,具体格式如下所示:

    scheme://host:port/path
    

    image.png

    举个具体的例子:

    假设这是我们用来调起App的网页html代码

    <html>
    <meta charset="UTF-8">
       <body>
         <h1>Test Scheme</h1> <!--自动加载隐藏页面跳转-->
          <!--手动点击跳转-->
          <a href="union://myunion:7380/ucar/user">点击打开APP</a>
       </body>
    </html>
    

    然后在App入口Activity(我这里是MainActivity)的注册清单AndroidManifest.xml中配置intent-filter,指定App要接收到的hostscheme,具体代码如下:

            <activity
                android:name="com.uu.genauction.view.activity.MainActivity"
                android:screenOrientation="sensorPortrait">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
                <intent-filter>
                    <!-- 协议部分配置 ,要在web配置相同的-->
                    <data
                        android:host="myunion"
                        android:scheme="union"/>
                    <category android:name="android.intent.category.DEFAULT"/>
                    <category android:name="android.intent.category.BROWSABLE"/>
                    <action android:name="android.intent.action.VIEW"/>
                </intent-filter>
            </activity>
    

    最后,当我们点击html的时候,实际上就是打开URIunion://myunion:7380/ucar/user。所以当与App上事先配置的信息一致时,就能够调起我们的App。

    另外,如果想把网页的信息带过去App的话,可以在URI中的path中添加参数,添加方式是?key=value。例如:

    union://myunion:7380/ucar/user?name=helloworld
    

    然后App端接收传过来的数据

      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mTextView= (TextView) findViewById(R.id.tv_main_show);
            Intent intent = getIntent();
            Uri uri=intent.getData();
            if(uri!=null){
                String name=uri.getQueryParameter("name");
                String scheme= uri.getScheme();
                String host=uri.getHost();
                String port=uri.getPort()+"";
                String path=uri.getPath();
                String query=uri.getQuery();
                mTextView.setText("获得的数据name="+name+"/r"+"scheme"+scheme+"/r"+"host" +
                        "host"+host+"/r"+"port"+port+"/r"+"path"+path+"/r"+"query"+query);
            }
      }
    
切记需要设置android:launchMode="singleTask",不然app会重新启动

设置后在这里接收参数:

kotlin:

override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)

    if ((intent?.data) != null) {

      val query = intent!!.data!!.query

      val BATTERY_CHANNEL = "com.zp.swap/fromIOSorAndroid"
      MethodChannel(flutterView,BATTERY_CHANNEL).invokeMethod("sendToFlutter",query)
    }
  }

java:

@Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);



        if (intent.getData() != null) {
            String query = intent.getData().getQuery();

            String BATTERY_CHANNEL = "com.zp.swap/fromIOSorAndroid";
            new MethodChannel(getFlutterView(), BATTERY_CHANNEL).invokeMethod("sendToFlutter",query);


            System.out.println("query===>" + query);

        }else {
//            String BATTERY_CHANNEL = "com.zp.swap/fromIOSorAndroid";
//            new MethodChannel(getFlutterView(), BATTERY_CHANNEL).invokeMethod("sendToFlutter","没有参数");

        }
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值