本文来自http://blog.csdn.net/liuxian13183/,引用必须注明出处!


注:此文尚未编辑完成,手滑发出,勿喷~感谢!


大的方向上

第一、使用进程共享的方式,往往使用android:process=remote,这样开启一个新的进程,使得所有进程都可以访问这个进程,使服务可以在多进程共享;而android:process=:remote相当于给当前进程一个私有进程,用来维护其自身的业务处理。开启新进程可以用在activity、service、broadcastReceiver、ContentProvider等组件。

If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the service runs in that process. If the process name begins with a lowercase character, the service will run in a global process of that name, provided that it has permission to do so. This allows components in different applications to share a process, reducing resource usage.

第二、使用广播的方式,可以设置category、component、package来区别广播,同时使用自定义权限来做限制

方法1、使用AIDL的方式进行

第三、使用Message+Binder

第四、使用Socket

第五、共享内存,如ContentProvider

linux有管道(Pipe)、信号(Signal)、报文(Message)、跳跃(Trace),其次信号量(Semaphore)、ShareMemory

Binder能支持跨进程通信的原因,是它实现IBinder接口,系统定义实现此接口即赋予进程通信的功能。

优势:做数据拷贝只用一次,则Pipe、Socket都需要两次;其次安全性高,不会像Socket会暴露地址,被人替换;

通信机制:Service向ServiceManager注册,得到虚拟的Uid和Pid,使用Binder通信;Client向ServiceManager请求,得到虚拟的Uid和Pid,以及目标对象的Proxy,底层通过硬件协议传输,使用Binder通讯。

通信时Client手持Proxy,ServiceManger进行转换,调用到Service。三者运行在三个独立进程中。Client/Sever全双工,互为Sever/Client

获得进程是否位于前台:

    public static boolean isRunningForeground(Application application) {        ActivityManager activityManager = (ActivityManager) application.getSystemService("activity");        List appProcessInfos = activityManager.getRunningAppProcesses();        Iterator var3 = appProcessInfos.iterator();        ActivityManager.RunningAppProcessInfo appProcessInfo;        do {            if (!var3.hasNext()) {                return false;            }            appProcessInfo = (ActivityManager.RunningAppProcessInfo) var3.next();        }        while (appProcessInfo.importance != 100 || !appProcessInfo.processName.equals(application.getApplicationInfo().processName));        return true;    }


更多相关文章

  1. Android(安卓)照相机实现方式
  2. activity和service之间的相互通信方法
  3. Android(安卓)Service、Messenger进程间通信
  4. Activity的四种加载方式
  5. android与服务器交互方式
  6. Eclipse无法安装ADT插件的解决
  7. Android(安卓)内存修改
  8. Android(安卓)sdk 安装及Adk安装
  9. Android(安卓)-- Vold机制简要分析

随机推荐

  1. 纯CSS3骏马奔腾动画特效
  2. 用 Jest 进行 JavaScript 测试[每日前端
  3. CSS3实现摩天轮动画特效
  4. CSS3和SVG实现的圆环菜单动画
  5. 膜拜!感谢大佬们赏饭吃,向你们致敬!
  6. 使用 React 要懂的 JavaScript 特性[每日
  7. Android使用Canvas绘图
  8. 这是个超棒的jQuery颜色选择器
  9. HTML5 电子书翻页 3D 动画特效
  10. HTML5 WebGL 3D星云动画特效