IPC__ALL

作者: jacky123 | 来源:发表于2016-06-16 14:37 被阅读26次

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

相关文章

  • IPC__ALL

    IPC机制的使用场景: Android对单个应用使用的最大内存做了限制,需要获取更多的内存 当前应用需要向其他应用...

  • Binder 和 AIDL

    Link: IPC__ALL 前言 Binder 是 Android中的一个类,它实现了 IBinder接口。从I...

网友评论

      本文标题:IPC__ALL

      本文链接:https://www.haomeiwen.com/subject/ahmmdttx.html