IPC__ALL

IPC机制的使用场景:

  • Android对单个应用使用的最大内存做了限制,需要获取更多的内存
  • 当前应用需要向其他应用获取数据

开启多进程

开启多进程模式的唯一方法是在清单文件中设置 process 属性。

  1. 私有进程的开启
android:process=":remote"
  1. 全局进程的开启
android:process="com.lzx.app.remote"

其他应用可以通过 ShareUID 方式和它跑在同一进程中。


多线程示例

开启多线程。

<application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:configChanges="orientation|screenSize"
            android:label="@string/app_name"
            android:launchMode="standard" >
             <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <!-- 当SecondActivity启动的时候,系统会为它创建一个单独私有的进程 -->
        <activity
            android:name=".SecondActivity"
            android:configChanges="screenLayout"
            android:label="@string/app_name"
            android:process=":remote" />
            
        <!-- 当ThirdActivity启动的时候,系统会为它创建一个全局的进程 -->
        <activity
            android:name=".ThirdActivity"
            android:configChanges="screenLayout"
            android:label="@string/app_name"
            android:process="com.ryg.chapter_2.remote" />
</application>

在MyApplication中

public class MyApplication extends Application {

    private static final String TAG = "MyApplication";

    @Override
    public void onCreate() {
        super.onCreate();
        String processName = MyUtils.getProcessName(getApplicationContext(),
                Process.myPid());
        Log.d(TAG, "application start, process name:" + processName);
    }
}

public class MyUtils {

    public static String getProcessName(Context cxt, int pid) {
        ActivityManager am = (ActivityManager) cxt
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if (runningApps == null) {
            return null;
        }
        for (RunningAppProcessInfo procInfo : runningApps) {
            if (procInfo.pid == pid) {
                return procInfo.processName;
            }
        }
        return null;
    }
}

启动Application,然后依次手动点击启动SecondActivity,ThirdActivity。我们可以看到log日志

Paste_Image.png

Binder 和 AIDL

ContentProvider

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

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,090评论 25 709
  • 多进程概念和多进程开发模式中常见问题 Android序列化机制和Binder 详细介绍Bundle、文件共享、AI...
    Danny_yy阅读 4,112评论 0 1
  • 一、Android IPC简介 IPC是Inter-Process Communication的缩写,含义就是进程...
    SeanMa阅读 5,920评论 0 8
  • 宝宝昨天晚上8点就睡了,澡都没洗,一觉睡到中午12点起来,起来帮他从头洗一次,之前朋友约好今天去打球,下午就和宝宝...
    一键转发_a805阅读 994评论 0 0
  • 1989年,我出生在河北廊坊。在我的记忆里,这座城市有窄窄的街道,有蓝蓝的天空,还有每天非常忙碌的妈妈。 三岁那年...
    小宇的闲聊时间阅读 3,870评论 1 1