首页 > 学院 > 开发设计 > 正文

【跨进程】跨进程通信---Activity

2019-11-09 15:18:09
字体:
来源:转载
供稿:网友

Acticity的跨进程访问,简单说就是跳转,用到Intent。

注意:跳转不需要指定Context对象和Activity的Class对象。而是指定Action,有的需要Uri。

错误姿势:

Intent Intent = new Intent(this,BActivity.class);startActivity(intent);

正确姿势(打电话):

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:110");startActivity(intent);

同理,程序调用相机,图库,短信,录音等功能,都可以理解为Activity间的跨进程通信。

以上,是自己编写的程序,跨进程访问Android系统程序,下面我们说一下跨进程访问非Android系统程序。

举个栗子:A程序访问B程序。

1.先创建B程序,建立一个新的Activity,暂且叫做OpenActivity,打开清单配置文件manifest,给OpenActivity增加intent-filter:

注意:A程序中,跨进程访问方法Intent intent = new Intent(action,data)

<action/>标签对应参数action

<data/>标签对应参数data。

<activity            android:name=".OpenActivity"            android:launchMode="singleTop">            <intent-filter>                <!--路径匹配  协议://主机名:端口号/路径-->                <!--content://com.wgl.share:8080/openApp-->                <data                    android:host="com.wgl.share"                    android:pathPRefix="/openApp"                    android:scheme="content" />                <action android:name="android.intent.action.VIEW"/>                <category android:name="android.intent.category.DEFAULT"/>                <category android:name="android.intent.category.BROWSABLE"/>            </intent-filter>        </activity>接下来,将B程序安装到手机。

2.再创建A程序,随意写一个点击事件,目的:访问并打开B程序

参数Intent.ACTION_VIEW,对应B程序中<action/>标签参数Uri.parse("content://com.wgl.share:8080/openApp"),对应B程序中<action/>标签

try {                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.wgl.share:8080/openApp"));                    startActivity(intent);                } catch (Exception e) {                    //本地没有B程序,请先安装                }安装A程序,触发点击事件,成功访问B程序。看效果:


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表