Android IPC机制(一)开启多进程

来源:互联网 发布:淘宝在那开通蚂蚁花呗 编辑:程序博客网 时间:2024/06/03 12:51


1. 为何要开启多进程 
为何开启Android应用要开启多进程,主要有以下几点:

  • 单进程所分配的内存不够,需要更多的内存。在早期android系统只为一个单进程的应用分配了16M的可用内存,随着手机的硬件的提升和android系统的改进,虽然可分配内存越来越多,但仍旧可以通过开启多进程来获取更多的内存来处理自己App的业务

  • 独立运行的组件,比如个推,它的服务会另开一个进程。

  • 运行一些”不可见人”的操作,比如获取用户的隐私数据,比如双守护进程来防止被用户杀掉

2. 开启多进程 
首先我们写一个Activity并启动一个service

public class MyProcessActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_my_process);        Intent myServiceIntent=new Intent(MyProcessActivity.this,MyService.class);        this.startService(myServiceIntent);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

service的代码:

public class MyService extends Service {    private static final String TAG = "wangshu";    @Override    public void onCreate() {        Log.i(TAG,"MyService is oncreate");    }    @Override    public int onStartCommand(Intent intent, int flags, int startId) {        Log.i(TAG, "MyProcessActivity is created: ");        return START_STICKY;    }    @Override    public void onDestroy() {        Log.i(TAG,"OnDestory");    }    @Override    public IBinder onBind(Intent arg0) {        return null;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

最后我们只需要在AndroidManifest.xml中的配置 android:process就可以了

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.liuwangshu.myprogress" >    <application        android:allowBackup="true"        android:name=".MyApplication"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme" >        <activity android:name=".MyProcessActivity" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <service            android:name=".MyService"            android:label="@string/app_name"            android:process=":remote">        </service>    </application></manifest>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

这里选择“remote”这个名字是随意主观的,你也可以取其他的名字。冒号“:”则代替当前应用的包名,所以MyService跑在进程名为“com.example.liuwangshu.myprogress:remote”的进程中。我们也可以设置 android:process=”com.example.liuwangshu.myprogress.remote”,这样MyService跑在进程名为“com.example.liuwangshu.myprogress.remote”的进程中。这两种命名也是有区别的,如果被设置的进程名是以一个冒号开头的,则这个新的进程对于这个应用来说是私有的,当它被需要或者这个服务需要在新进程中运行的时候,这个新进程将会被创建。如果这个进程的名字是以小写字符开头的,则这个服务将运行在一个以这个名字命名的全局的进程中,当然前提是它有相应的权限。这将允许在不同应用中的各种组件可以共享一个进程,从而减少资源的占用。

我们运行应用则发现:开启了两个进程 
这里写图片描述

3. 开启多进程引出的问题 
开启多进程会使Application运行两次,我们继承Application,在oncreate方法中打log并运行程序

public class MyApplication extends Application {    private static final String TAG = "wangshu";    @Override    public void onCreate() {        super.onCreate();        int pid = android.os.Process.myPid();        Log.i(TAG, "MyApplication is oncreate====="+"pid="+pid);    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

这里写图片描述

在log中我们发现我们开启的两个进程都会执行oncreate方法。现在很多开发者都习惯在Application中做初始化操作以及数据的传递操作,这显然是不妥当的,解决的方法就是得到每个进程的名称,如果进程的名称和我们应用的进程名称相同则做我们应用的操作,如果不是则做其他进程的操作

public class MyApplication extends Application {    private static final String TAG = "wangshu";    @Override    public void onCreate() {        super.onCreate();        int pid = android.os.Process.myPid();        Log.i(TAG, "MyApplication is oncreate====="+"pid="+pid);        String processNameString = "";        ActivityManager mActivityManager = (ActivityManager)this.getSystemService(getApplicationContext().ACTIVITY_SERVICE);        for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager.getRunningAppProcesses()) {            if (appProcess.pid == pid) {                processNameString = appProcess.processName;            }        }        if("com.example.liuwangshu.myprogress".equals(processNameString)){            Log.i(TAG, "processName="+processNameString+"-----work");        }else{            Log.i(TAG, "processName="+processNameString+"-----work");        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

这里写图片描述

从Log中可以看到不同的进程执行了不同的操作。

github源码下载

0 0
原创粉丝点击