Android Intent的隐示启动(启动其他APP界面并传递数据)

引用

1、Android中Intent的隐式启动和显示启动Activity
2、使用Intent来进行App间的基本交互
3、(备忘)Android app中调用启动其他应用(系统应用和第三方应用)

基础

概念

  • 显式启动:直接指定要跳转的Activity类名,不用过滤,效率高,适用于同一个应用中的不同Activity跳转
  • 隐式启动:需要过滤,相对耗时,但可以找到所有之匹配的应用。适用于不同应用之间的Activity跳转。

属性

  • component(组件):目的组件
  • action(动作):用来表现意图的行动
  • category(类别):用来表现动作的类别
  • data(数据):表示与动作要操纵的数据
    1、android:schema=用于指定数据的协议部分,如上例中的http部分
    2、android:host=用于指定数据的主机名部分,如上例中的www.baidu.com部分
    3、android:port=用于指定主机名和端口之后的部分,一般紧随在主机名之后
    4、android:mimeType=用于指定可以处理的数据类型,允许使用通配符的方式进行指定
  • type(数据类型):对于data范例的描写
  • extras(扩展信息):扩展信息
  • Flags(标志位):期望这个意图的运行模式

截图

implicit_activity.png

代码

一、同一个APP内

1、显示启动:最最最普通的启动啦~~
Intent it = new Intent(instance, FirstActivity.class);
startActivity(it);
2、隐示启动:操作系统的一些界面
  • 打开系统设置
//action = Settings.ACTION_SETTINGS = android.settings.SETTINGS
Intent it = new Intent(Settings.ACTION_SETTINGS);
startActivity(it);
  • 打电话:这里就有action动作和设置传递的data数据
//action = Intent.ACTION_DIAL = android.intent.action.DIAL
Intent it = new Intent();
it.setAction(Intent.ACTION_DIAL);
it.setData(Uri.parse("tel:10086"));//定位
startActivity(it);
  • 开启个网页:这个就可能涉及到多个浏览器同时获得了,有可能UC浏览器啊,百度浏览器啊同时就接到了这个action动作指令,并能打开这种http的url
 Intent it = new Intent();
it.setAction(Intent.ACTION_VIEW);
it.setData(Uri.parse("http://www.baidu.com"));
startActivity(it);
3、隐示启动:打开APP内其他活动页
  • 同一个action被多个activity活动页接受的情形
    1、manifest清单文件中有两个activity:XXX和YYY
    2、XXX和YYY都有相同的\color{blue}{action=com.test.implicit}
    3、\color{blue}{category不同,一个是xxx,一个是yyy}
    4、暂无data和type
    5、\color{red}{常重要:必须写默认的category,否则就指向不到此活动}
    \color{red}{<category android:name="android.intent.category.DEFAULT"/>}
    6、\color{red}{如果对外的话需要 android:exported="true"}
app2.png
<activity android:name=".module.study.components.activity.ex_or_implicit.XXXActivity"
            android:screenOrientation="portrait">
            <!--隐示启动默认带的类别就是:android.intent.category.DEFAULT,必须要加-->
            <intent-filter>
                <action android:name="com.test.implicit"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="xxx"/>
            </intent-filter>
        </activity>
        <activity android:name=".module.study.components.activity.ex_or_implicit.YYYActivity"
            android:screenOrientation="portrait">
            <!--隐示启动默认带的类别就是:android.intent.category.DEFAULT,必须要加-->
            <intent-filter>
                <action android:name="com.test.implicit"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="yyy"/>
            </intent-filter>
        </activity>
//这个action在manifest中显然会匹配到两个activity,然后会让你选择一个
String action_xxx_or_yyy = "com.test.implicit";
Intent it = new Intent();
it.setAction(action_xxx_or_yyy);
startActivity(it);
  • 同一个action,又被category过滤了一遍,指向一个activity活动页
String action_xxx = "com.test.implicit";
String category_xxx = "xxx";
Intent it = new Intent();
it.setAction(action_xxx);  //动作
it.addCategory(category_xxx); //类别
startActivity(it);

二、另一个APP

1、显示启动:开启特定APP,全路径:2个参数:applicationId包名和工程目录下对应的画面
//method 1
Intent it = new Intent();
it.setClassName("com.xxx","com.xxx.MainActivity");
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(it);

//method 2 带action表示app的启动页面:ACTION_MAIN
Intent it = new Intent();
ComponentName cn = new ComponentName("com.xxx","com.xxx.MainActivity");
it.setComponent(cn);
it.setAction(Intent.ACTION_MAIN);//android.intent.action.MAIN
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(it);

\color{red}{此处有坑,如果是多渠道打包的apk,applicationId和工程目录路径不一致的话}
多渠道的 applicationId = com.jiangsu.nantong.testapp ,实际工程目录的根目录是com.china.testapp

Intent it = new Intent();
it.setClassName("com.jiangsu.nantong.testapp","com.china.testapp.MainActivity");
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(it);
2、隐示启动:开启某个APP活动,都会被过滤到action的情景
other_app2.png
  • 两个APP定义一样的action,不同的category
    MyApp
<activity android:name=".ImplicitActivity"
            android:exported="true">
            <intent-filter>
                <!--action 一样的-->
                <action android:name="test_implicit"/>
                <!--category 多渠道每个不一样-->
                <category android:name="my_category"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

YourApp

<activity android:name=".ImplicitActivity"
            android:exported="true">
            <intent-filter>
                <!--action 一样的-->
                <action android:name="test_implicit"/>
                <!--category 多渠道每个不一样-->
                <category android:name="your_category"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

HelloApp 隐示过滤启动,找到2个

//这个action在manifest中显然会匹配到两个activity,然后会让你选择一个
String test_implicit = "test_implicit";
Intent it = new Intent();
it.setAction(test_implicit);
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(it);
3、隐示启动:开启特定APP活动,并互相传递数据
  • 这里由action和category2层过滤,找到特定的app的活动,传递数据的话和以前一样
//隐示启动:指定某个APP的活动
    private void startImplicitOtherSingleApp() throws Exception{
        String test_implicit = "test_implicit";
        String category_your = "your_category";
        Intent it = new Intent();
        it.setAction(test_implicit);  //动作
        it.addCategory(category_your); //类别
        //it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        it.putExtra("msg","Hello man, I'm HelloApp, Are you ok?");
        startActivityForResult(it,1001);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        LogUtil.i("requestCode = "+requestCode + ", resultCode = "+);
        if(resultCode != RESULT_OK){
            return;
        }

        if(requestCode == 1001){
            String msg = data.getStringExtra("msg");
            LogUtil.i(msg);
            ToastUtil.showShort(instance, msg);
        }
        //super.onActivityResult(requestCode, resultCode, data);
    }

三、其他

1、启动APP页面一般要询问是否有,提供几个方法吧
/**
     * 检测某个应用是否安装
     *
     * @param context
     * @param packageName
     * @return
     */
    public static boolean isAppInstalled(Context context, String packageName) {
        try {
            context.getPackageManager().getPackageInfo(packageName, 0);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

    /**
     * 检测某个应用是否安装
     *
     * @param context
     * @param intent
     * @return
     */
    public static boolean isAppInstalled(Context context, Intent intent){
        List acts = context.getPackageManager().
                queryIntentActivities(intent,PackageManager.MATCH_DEFAULT_ONLY);
        return (acts.size() > 0);
    }


    /**
     * 检测某个应用是否安装
     *
     * @param context
     * @param intent
     * @return
     */
    public static boolean isAppInstalled2(Context context, Intent intent){
        return (intent.resolveActivity(context.getPackageManager()) != null);
    }
2、关于data下schema请看这篇文章Android Schema的妙用

-------------一年马上又要过去了,感觉很忙,又好像啥事都没干。。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 12,858评论 2 59
  • 一、概述 Activity 作为与用户交互的一个窗口,是使用非常频繁的一个基本组件。Android系统是通过Act...
    三也视界阅读 2,263评论 3 11
  • 1.Intent的用法: (1)Action跳转 1、 使用Action跳转,当程序AndroidManifest...
    彭文波_4118阅读 5,874评论 0 7
  • 曲终人醉,且抱花枝浅寐, 铃响惊梦,旅袋沾涎惭愧。 生活颓废,三顿两卧酗酒, 异想天开,一往牧丹玫瑰。
    云逸1108阅读 163评论 0 1
  • 文丨木小易 图丨网络 1 我想,每个人都曾经做过让自己懊悔的事情吧。或许是感情上的错过,或许是事业上的选择,或许...
    木小易512阅读 399评论 0 0