zoukankan      html  css  js  c++  java
  • 一手遮天 Android

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

    一手遮天 Android - ipc(跨进程通信): URLScheme(deep link)

    示例如下:

    /ipc/URLSchemeDemo1.java

    /**
     * 本例用于演示如何通过 URLScheme(deep link) 启动另一个 app
     * 正常演示此示例需要安装 AndroidDemoIpc 项目生成的 app
     *
     * 注:支持指定 URLScheme 的实现请参见 AndroidDemoIpc 项目中的 URLScheme1.java
     */
    
    package com.webabcd.androiddemo.ipc;
    
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.net.Uri;
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.widget.Button;
    import android.widget.TextView;
    
    import com.webabcd.androiddemo.R;
    
    import java.util.List;
    
    public class URLSchemeDemo1 extends AppCompatActivity {
    
        private Button mButton1;
        private Button mButton2;
        private TextView mTextView1;
        private WebView mWebView1;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_ipc_urlschemedemo1);
    
            mButton1 = findViewById(R.id.button1);
            mButton2 = findViewById(R.id.button2);
            mTextView1 = findViewById(R.id.textView1);
            mWebView1 = findViewById(R.id.webView1);
    
            sample();
        }
    
        // 判断指定的协议是否有效(即指定的 url 是否有 app 可以打开)
        private boolean schemeIsValid(String url) {
            PackageManager manager = this.getPackageManager();
            Intent action = new Intent(Intent.ACTION_VIEW);
            action.setData(Uri.parse(url));
            List list = manager.queryIntentActivities(action, PackageManager.GET_RESOLVED_FILTER);
            return list != null && list.size() > 0;
        }
    
        private void sample() {
            mButton1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    try {
                        Uri uri = Uri.parse("webabcd://a.b.c/api?p1=v1&p2=v2");
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    } catch (Exception ex) {
                        // 如果指定的协议无效的话则会抛出类似如下的异常
                        // android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=webabcd://a.b.c/api?p1=v1&p2=v2 }
                        mTextView1.setText(ex.toString());
                    }
                }
            });
    
            mButton2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // 判断指定的协议是否有效
                    boolean b1 = schemeIsValid("webabcd://a.b.c/api");
                    boolean b2 = schemeIsValid("abc://a.b.c/api");
                    mTextView1.append("webabcd://a.b.c/api, " + b1);
                    mTextView1.append("
    ");
                    mTextView1.append("abc://a.b.c/api, " + b2);
                }
            });
    
            // 在 html 中通过 js 打开指定的协议,从而唤起另一个 app
            WebSettings webSettings = mWebView1.getSettings();
            webSettings.setJavaScriptEnabled(true);
            mWebView1.loadUrl("file:///android_asset/URLSchemeDemo1.html");
        }
    }
    
    

    /layout/activity_ipc_urlschemedemo1.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            android:text="打开 webabcd://a.b.c/path?p1=v1&amp;p2=v2" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAllCaps="false"
            android:text="判断指定的协议是否有效" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    
        <WebView
            android:id="@+id/webView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    
    </LinearLayout>
    

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

  • 相关阅读:
    SQL server 和Oracle 序列
    AD 域服务简介(一)- 基于 LDAP 的 AD 域服务器搭建及其使用(转)
    Windows Server 2008 R2 搭建DNS服务器(转)
    windows7下搭建HTTP服务器
    解决vcenter 6.0 vcsa安装插件时报错的问题
    Windows server 2008R2系统登录密码破解
    vmware Horizon 7 与远程桌面(mstsc)兼容性问题解决办法
    VCSA服务重启命令
    如何哄女朋友开心(转)
    快速搭建一个本地的FTP服务器 win10及win7
  • 原文地址:https://www.cnblogs.com/webabcd/p/android_ipc_URLSchemeDemo1.html
Copyright © 2011-2022 走看看