Open qmsggg opened 6 years ago
可以
eg
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.qmsggg.apptest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:name=".MyApplication"
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>
<service android:name=".MyService" android:label="@string/app_name" android:process=":test"/>
<service android:name=".MyService2" android:process=":test2" android:label="123"/>
</application>
![image](https://user-images.githubusercontent.com/28669743/37896727-080d86e6-3117-11e8-88e2-43f766ed2138.png)
因为多进程首先会有多个Application,数据会被初始化多次,其次进程间通信比较麻烦,还有一个就是每个进程有单独的虚拟机,多个进程就会比较占内存
为何要开启多进程?
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
}
package com.example.qmsggg.apptest;
import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.support.annotation.Nullable; import android.util.Log;
/**
public class MyService extends Service {
}
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); } }
package com.example.qmsggg.apptest;
import android.app.ActivityManager; import android.app.Application; import android.util.Log;
/**
public class MyApplication extends Application { private static final String TAG = "qmsgggg"; @Override public void onCreate() { super.onCreate(); int pid = android.os.Process.myPid(); Log.i(TAG, "pid=" + pid);
}