多进程

进程与多进程是什么东东

  • 什么是进程?
    进程是一个活动,是线程的容器。可以想像成工厂里的车间,每个车间进行不同的活动。
  • 什么是多进程?
    多进程就是多个不同的车间
  • 什么时候使用多进程?
    当APP需要使用很大的内存而超过进程的内存限制值时,可以通过开辟多个进程来达到占用更大内存的目的。
  • 进程的创建?
    只需在Mainfest中在你需要的四大组件中加上
    android:prcocess="进程名(一般为包名)”
  • 进程的等级(等级越高越不容易被系统回收)
    前台进程
    可见进程
    服务进程
    后台进程
    空进程

多进程

  • 为什么要使用多进程?
    可以使APP更稳定,占用更多的内存。
  • 如何使用多进程?
    -使用多进程需要注意哪些地方?
    进程间内存的不可见性
    跨进程不能进行类型强转

多进程间的通信IPC

  • IPC(interprocess communication)
  • 为什么需要?
    内存不共享
  • 如何通信
    系统实现
    Messenger --利用Handler
    AIDL:Android Interface definition language
    在APP下创建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实现,然后
    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);
    }
    }
    创建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; }
    }

从封装变化 角度 对模式分类

  • 组件协作
    Template Method
    Strategy
    Observer/Event
  • 单一职责
    Decorator
    Bridge
  • 对象创建:
    Factory Method
    Abstract Factory
    Prototype
  • 对象性能

重构获得模式

  • 好的设计---应对变化,提高复用
  • 设计模式的是“寻求变化点”,然后在变化点处应用设计模式
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Jianwei's blog 首页 分类 关于 归档 标签 巧用Android多进程,微信,微博等主流App都在用...
    justCode_阅读 6,133评论 1 23
  • 如果一个进程占用内存超过了这个内存限制,就会报OOM的问题,很多涉及到大图片的频繁操作或者需要读取一大段数据在内存...
    trampcr阅读 16,998评论 10 65
  • 什么是进程 进程是一个具有独立功能的程序关于某个数据集合的一次运行活动。 进程是一个“执行中的程序”。程序是一个没...
    MrMagicWang阅读 1,486评论 1 1
  • 首先得提一下,什么是进程,很多人容易把进程和线程混淆了,这里简述下二者的概念。 进程是具有一定独立功能的程序,关于...
    一叶知秋yi阅读 2,174评论 0 1
  • 申城的清晨来的特别早 每个晴天里的每个清晨 第一缕阳光总会在天边热情绽放 早早地唤醒了这座沉睡了一夜的不夜城 刚安...
    寒奇阅读 381评论 0 0

友情链接更多精彩内容