進(jìn)程與多進(jìn)程是什么東東
- 什么是進(jìn)程?
進(jìn)程是一個活動,是線程的容器。可以想像成工廠里的車間,每個車間進(jìn)行不同的活動。 - 什么是多進(jìn)程?
多進(jìn)程就是多個不同的車間 - 什么時候使用多進(jìn)程?
當(dāng)APP需要使用很大的內(nèi)存而超過進(jìn)程的內(nèi)存限制值時,可以通過開辟多個進(jìn)程來達(dá)到占用更大內(nèi)存的目的。 - 進(jìn)程的創(chuàng)建?
只需在Mainfest中在你需要的四大組件中加上
android:prcocess="進(jìn)程名(一般為包名)” - 進(jìn)程的等級(等級越高越不容易被系統(tǒng)回收)
前臺進(jìn)程
可見進(jìn)程
服務(wù)進(jìn)程
后臺進(jìn)程
空進(jìn)程
多進(jìn)程
- 為什么要使用多進(jìn)程?
可以使APP更穩(wěn)定,占用更多的內(nèi)存。 - 如何使用多進(jìn)程?
-使用多進(jìn)程需要注意哪些地方?
進(jìn)程間內(nèi)存的不可見性
跨進(jìn)程不能進(jìn)行類型強(qiáng)轉(zhuǎn)
多進(jìn)程間的通信IPC
- IPC(interprocess communication)
- 為什么需要?
內(nèi)存不共享 - 如何通信
系統(tǒng)實(shí)現(xiàn)
Messenger --利用Handler
AIDL:Android Interface definition language
在APP下創(chuàng)建AIDLfile文件寫入如下代碼
// IMyAidlInterface.aidlpackage com.geekband.Test01;
// Declare any non-default types here with import statements
interface IMyAidlInterface {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString);
String getName(String nickName);
}
通過Terminal實(shí)現(xiàn),然后
public class AIDLActivity extends Activity {
ServiceConnection mServiceConnection = new ServiceConnection() {
@Override public void onServiceConnected(ComponentName name, IBinder service) {
mIMyAidlInterface = IMyAidlInterface.Stub.asInterface(service);
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
private IMyAidlInterface mIMyAidlInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aidl);
findViewById(R.id.button_aidl).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(mIMyAidlInterface != null){
try {
String name = mIMyAidlInterface.getName("nick_know_maco");
Toast.makeText(AIDLActivity.this, name + "", Toast.LENGTH_LONG).show();
} catch (RemoteException e) {
e.printStackTrace();
}
}
}
});
bindService(new Intent(this, AIDLService.class), mServiceConnection, Context.BIND_AUTO_CREATE);
}
}
創(chuàng)建Service類
public class AIDLService extends Service {
IMyAidlInterface.Stub mStub = new IMyAidlInterface.Stub(){
@Override
public void basicTypes(int anInt, long aLong,boolean aBoolean,
float aFloat, double aDouble, String aString) throws RemoteException {
}
@Override
public String getName(String nickName) throws RemoteException {
return nickName + "aidl_hahaha";
}
};
@Nullable
@Override
public IBinder onBind(Intent intent) {return mStub; }
}
從封裝變化 角度 對模式分類
- 組件協(xié)作
Template Method
Strategy
Observer/Event - 單一職責(zé)
Decorator
Bridge - 對象創(chuàng)建:
Factory Method
Abstract Factory
Prototype - 對象性能
重構(gòu)獲得模式
- 好的設(shè)計(jì)---應(yīng)對變化,提高復(fù)用
- 設(shè)計(jì)模式的是“尋求變化點(diǎn)”,然后在變化點(diǎn)處應(yīng)用設(shè)計(jì)模式