我们接着研究下SystemUI下的RecentActivity:
这个类就是用来清除当前正在使用的应用程序的。。。
去找那个清除按钮事件,看看调用了什么方法:
首先获取final ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);ActivityManager实例
然后final List runningProcesses = am.getRunningAppProcesses();获取当前的应用进程信息
然后是一个for来循环去清楚应用进程。
最后获取最近的任务信息队列:
final List recentTasks =
                        am.getRecentTasks(ActivityManager.getMaxRecentTasksStatic(), 
                            ActivityManager.RECENT_IGNORE_UNAVAILABLE);
通过for循环将他们从队列中移除。


这里让我想到了之前的一个问题?android4.0之后Service会随着app的结束而结束,我们怎么样才能让我们的Service一直运行在后台呢?就像QQ一样?


我们通过这个来找找有没有什么办法?
if ((sr.serviceInfo.flags&ServiceInfo.FLAG_STOP_WITH_TASK) != 0) {
                    Slog.i(TAG, "Stopping service " + sr.shortName + ": remove task");
                    stopServiceLocked(sr);
}
这里就是清除和App相关的Service的代码?
这里有个属性可以注意下(sr.serviceInfo.flags&ServiceInfo.FLAG_STOP_WITH_TASK) != 0 什么情况下可以避免这个条件成立。
/**
     * Bit in {@link #flags}: If set, the service will automatically be
     * stopped by the system if the user removes a task that is rooted
     * in one of the application's activities.  Set from the
     * {@link android.R.attr#stopWithTask} attribute.
     */
    public static final int FLAG_STOP_WITH_TASK = 0x0001;


    /**
     * Bit in {@link #flags}: If set, the service will run in its own
     * isolated process.  Set from the
     * {@link android.R.attr#isolatedProcess} attribute.
     */
    public static final int FLAG_ISOLATED_PROCESS = 0x0002;


    /**
     * Bit in {@link #flags}: If set, a single instance of the service will
     * run for all users on the device.  Set from the
     * {@link android.R.attr#singleUser} attribute.
     */
    public static final int FLAG_SINGLE_USER = 0x40000000;


似乎只要设置这个属性为FLAG_ISOLATED_PROCESS就能够避免被清楚,真的是这样嘛?只有试试才知道!!!

更多相关文章

  1. 一款霸榜 GitHub 的开源 Linux 资源监视器!
  2. Andorid中xml资料汇总
  3. Android中GridView的一些特殊属性
  4. Android(安卓)技巧 - 取得当前活动的activity的class name / pac
  5. Android(安卓)Vector(Vector/Vector动画)
  6. Android(安卓)ORM-GreenDao学习之一基础篇
  7. Android控制ScrollView滚动
  8. android如何让service不被杀死-提高进程优先级
  9. Android自定义View

随机推荐

  1. Android控件Editext、TextView属性详解
  2. Android服务器搭建Git+Repo+Gerrit
  3. Android面试准备(中高级)
  4. Android(安卓)系统分区分析
  5. Android(安卓)- Android(安卓)Studio 的
  6. Android(安卓)系统信息获取(CPU,RAM,ROM,
  7. 获取系统语言的方法
  8. Mac 搭建Android开发环境
  9. android 5.1拍照后图片镜像处理
  10. Android开发:还是Menu