Android进程间互相调用,一个APP调用另外的APP

本文详细介绍了Android系统中进程间的三种通信方式:通过注册系统服务实现的AIDL通信、通过直接指定目标包名来启动Activity的方法以及通过配置特定的Intent过滤器使应用支持外部调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

进程间的调用我觉得可以总结为三种:一种是注册系统服务,一种是调用package包名,最后一种是应该也是类似注册系统服务的方式,具体不知道什么方式。

一、注册系统服务

AIDL和调用系统的相册、拍照的时候都需要启动一个intent,指定action,而在AIDL中,如果要是server能够被调用的话,就需要在manifest文件里面注册系统服务,所以我觉得调用系统的相册、拍照等功能的时候也是在系统注册了服务,就把 这两种归结为一种。

server注册系统服务

 <service android:name="com.example.remoteservice.MyRemeoteService">
            <!-- 进程间的AIDL需要进行配置intent-filter -->
            <intent-filter>
                <action android:name="com.example.remoteservice.action" />
                
            </intent-filter>
            
        </service>

client调用服务

Intent intent = new Intent("com.example.remoteservice.action");
......

......

. .....
bindService(intent, conn, Context.BIND_AUTO_CREATE);

注意:其中client调用的action:com.example.remoteservice.action,就是server注册的actioncom.example.remoteservice.action;

二、调用package包名:这种方法是网上查到的,写过来总结一下

调用端:

//启动别的进程的activity
ComponentName componetName = new ComponentName(  
               //这个是另外一个应用程序的包名  
               "com.woxingwoxiu.showvideo.activity",  
               //这个参数是要启动的Activity  
               "com.woxingwoxiu.showvideo.activity.MainMyInfoActivity");  
        
           try {  
               Intent intent = new Intent();  
               intent.setComponent(componetName);  
               startActivity(intent);  
           } catch (Exception e) {  
             Toast.makeText(getApplicationContext(), "可以在这里提示用户没有找到应用程序,或者是做其他的操作!", 0).show();  
                 
           } 

被调用端:

 <activity
            android:name="com.woxingwoxiu.showvideo.activity.MainMyInfoActivity"
            android:configChanges="keyboardHidden|orientation"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoTitleBar"
            android:windowSoftInputMode="adjustUnspecified|stateHidden" >
            //需要调用的activity需要添加的action
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
            </intent-filter>
            </activity>

注意:需要在被调用的activity里面添加action:android.intent.action.MAIN,要不然会报java.lang.SecurityException: Permission Denial: starting Intent,的错误;这种方式的调用也可以进行数据交互,具体的交互跟activity之间的数据交互是一样的,用intent传输数据。

三、这种是我在播放器的时候用到的,具体还不知道到底底层是用什么方式做的,不过推测应该也是类似注册的方式做的。需要在manifest文件里面添加对应的东西

代码:

 <activity android:name=".activity.System" android:screenOrientation="landscape" android:configChanges="keyboard|screenSize|orientation">
            <!-- 让其他应用可以调用我的播放器start-->
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />


                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />


                <data android:scheme="rtsp" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />


                <data android:mimeType="video/*" />
                <data android:mimeType="application/sdp" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />


                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />


                <data android:scheme="http" />
                <data android:mimeType="video/mp4" />
                <data android:mimeType="video/3gp" />
                <data android:mimeType="video/3gpp" />
                <data android:mimeType="video/3gpp2" />
            </intent-filter>
            <!-- 让其他应用可以调用我的播放器-end-->
</activity>


挖下坑,以后懂了什么原理再来埋。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值