Android 多进程模式
Android 中通过Binder可以实现进程间通信。通过Socket可以实现任意两个终端之间的通信,同一个设备上的两个进程也可以通过Socket通信。
Android 多进程的情况可分为两种:第一种情况是一个应用因为某些原因自身需要采用多进程模式来实现,至于原因,可能有很多,比如有些模块由于特殊原因需要运行在单独的进程中,又或者为了加 大一个应用可使用的内存所以需要通过多进程来获取多份内存空间。Android对单个应用所 使用的最大内存做了限制,早期的一些版本可能是16MB,不同设备有不同的大小。另 一种情况是当前应用需要向其他应用获取数据,由于是两个应用,所以必须采用跨进程的方式来获取所需的数据。
- 多进程模式开启以及运行机制
1 多进程开启
在Android中使用多进程只有一种方法,就是给四 大组件(Activity、Service、Receiver、ContentProvider)在 AndroidMenifest 中指定 android:process 属性,因此也就是说我们无法给一个线程或者一个实体类指定其运行时所在的进程。
多进程开启manifest中代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hp.ipctest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!--:remote的含义是指要在当前的进程名前面附加上当前的包名-->
<!--进程这样定义属于当前应用的私有进程,其他应用的组件不可以和它跑在同一个进程中-->
<activity
android:name=".SecondActivity"
android:label="@string/app_name"
android:process=":remote"/>
<!--com.example.hp.ipctest.remote 完整的命名方式,不会增加包名信息-->
<!--进程这样定义属于全局进程,其他应用通过ShareUID方式可以和它跑在同一个进程中-->
<activity
android:name=".ThirdActivity"
android:label="@string/app_name"
android:process="com.example.hp.ipctest.remote"/>
</application>
</manifest>
进程信息查看结果
C:\Users\HP>adb shell
shell@MA5:/ $ ps |grep ipctest
u0_a109 8888 305 1541448 60664 SyS_epoll_ 0000000000 S com.example.hp.ipctest
u0_a109 8984 305 1673420 77916 SyS_epoll_ 0000000000 S com.example.hp.ipctest:remote
u0_a109 9013 305 1566624 62436 SyS_epoll_ 0000000000 S com.example.hp.ipctest.remote
shell@MA5:/ $
2 多进程机制
使用多进程会造成如下几方面的问题
(1)静态成员和单例模式完全失效。
(2)线程同步机制完全失效。
(3) SharedPreferences 的可靠性下降。
(4) Application会多次创建。
静态成员失效问题代码示例
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UserManager.sUserId=2;
Button startSecond = findViewById(R.id.start_second);
startSecond.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.start_second:
Log.e(TAG, "sUserId:"+UserManager.sUserId);
Intent intent =new Intent(this,SecondActivity.class);
startActivity(intent);
break;
default:
break;
}
}
}
SecondActivity
public class SecondActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "SecondActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Log.e(TAG, "sUserId:"+UserManager.sUserId );
UserManager.sUserId=3;
Button startThird = findViewById(R.id.start_third);
startThird.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.start_third:
Intent intent =new Intent(this,ThirdActivity.class);
startActivity(intent);
break;
default:
break;
}
}
}
UserManager
public class UserManager {
public static int sUserId=1;
}
打印结果
10661-10661/com.example.hp.ipctest E/MainActivity: sUserId:2
10726-10726/com.example.hp.ipctest:remote E/SecondActivity: sUserId:1
Application多次创建代码示例
MyApplication 代码
public class MyApplication extends Application {
private static final String TAG = "MyApplication";
private String procName;
@Override
public void onCreate() {
super.onCreate();
ActivityManager mActivityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager
.getRunningAppProcesses()) {
if (appProcess.pid == android.os.Process.myPid()) {
procName = appProcess.processName;
}
}
Log.e(TAG, "process name:"+procName);
}
}
打印结果
14280-14280/? E/MyApplication: process name:com.example.hp.ipctest
14739-14739/? E/MyApplication: process name:com.example.hp.ipctest:remote
15194-15194/? E/MyApplication: process name:com.example.hp.ipctest.remote
参考内容:Android 开发艺术探索(任玉刚)