zoukankan      html  css  js  c++  java
  • RK 清理后台所有历史App任务

    一.OS Android5.1

    1.1.应用是否具有android.intent.category.LAUNCHER属性有关,在主Activity有LAUNCHER的前提下,android:excludeFromRecents=“true”,才能达到在最近任务列表中隐藏该应用的目的

    1.2.frameworksasepackagesSystemUIsrccomandroidsystemuistatusbaroswinRecentAppManager.java

    	public void removeRecentApp(String packName){
            ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
    		PackageManager pm = mContext.getApplicationContext().getPackageManager();
            List<ActivityManager.RecentTaskInfo> appTask = am.getRecentTasks(50,ActivityManager.RECENT_WITH_EXCLUDED|ActivityManager.RECENT_IGNORE_UNAVAILABLE);
            if(!appTask.isEmpty()){
                try {
                    for(ActivityManager.RecentTaskInfo ra : appTask){
                        Intent intent = new Intent(ra.baseIntent);
                        if((isCurrentHomeActivity(intent.getComponent().getPackageName(), null))){ 
                            continue;
                        }
    
                        if(intent.getComponent().getPackageName().equals(packName)){
                            int persistentId = ra.persistentId; // pid 
                            Log.d(TAG, "removeRecentApp --> packName:" + packName);
                            am.removeTask(persistentId/*, ActivityManager.REMOVE_TASK_KILL_PROCESS*/);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
             }
    	}
    
    
        public void clearRunningTasks(){
            ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RecentTaskInfo> run = am.getRecentTasks(512, ActivityManager.RECENT_IGNORE_UNAVAILABLE);
            PackageManager pm =mContext.getPackageManager();
            try {
                for(ActivityManager.RecentTaskInfo ra : run){
                    Intent intent = new Intent(ra.baseIntent);
                    if((isCurrentHomeActivity(intent.getComponent().getPackageName(), null))
                    		||(intent.getComponent().getPackageName().equals("com.android.launcher"))
                    		|| (intent.getComponent().getPackageName().equals("xxxx.xxxx.xxx"))){
                     continue;
                    }
    
    
                    int persistentId = ra.persistentId;
                    am.removeTask(persistentId/*, ActivityManager.REMOVE_TASK_KILL_PROCESS*/);
     			 Toast.makeText(mContext, "Clear Recent APP", 500).show();
                }
            } catch (Exception e) {
               e.printStackTrace();
            }
        }
    

    1.3. 发送广播 清理后台所有历史App任务

    frameworksasepackagesSystemUIsrccomandroidsystemui ecentCloseTaskReceiver.java

    package com.android.systemui.recent;
    
    import com.android.systemui.recent.RecentsPanelView;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    import android.app.ActivityManager;
    import android.content.pm.PackageManager;
    
    import java.util.List;
    
    public class CloseTaskReceiver  extends BroadcastReceiver {
    
    	private  Context mContext;
    	private  String packName ="bd.nj.onetoonecall";
    	static final String BOOT_COMPLETED = "android.intent.action.BOOT_COMPLETED";
    
        @Override
        public void onReceive(Context context, Intent intent) {
        	mContext = context;
    		String action = intent.getAction();
    		if (BOOT_COMPLETED.equals(action)){
    			Log.d("gatsby","BOOT_COMPLETED removeRecentApp");
    			removeRecentApp();
    		}
    		if (("com.xinhua.closeallapp").equals(action)) {
    			 Log.d("gatsby","com.xinhua.closeallapp");
    			 removeRecentApp();
    		}
    	}
    
    	public void removeRecentApp(){
    		ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
    		PackageManager pm = mContext.getApplicationContext().getPackageManager();
    		List<ActivityManager.RecentTaskInfo> appTask = am.getRecentTasks(50,ActivityManager.RECENT_WITH_EXCLUDED|ActivityManager.RECENT_IGNORE_UNAVAILABLE);
    		if(!appTask.isEmpty()){
    			try {
    				for(ActivityManager.RecentTaskInfo ra : appTask){
    					Intent intent = new Intent(ra.baseIntent);
    					if(!intent.getComponent().getPackageName().equals(packName)){
    						int persistentId = ra.persistentId; // pid
    						am.removeTask(persistentId/*, ActivityManager.REMOVE_TASK_KILL_PROCESS*/);
    					}else{
    						Log.d("gatsby", "removeRecentApp --> packName:" + packName);
    					}
    				}
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
    

     1.4.清单文件 自定义广播

    			
    	<receiver
                android:name=".recent.CloseTaskReceiver">
                <intent-filter>
                    <action android:name="com.xinhua.closeallapp" />
    		<action android:name="android.intent.action.BOOT_COMPLETED" />
                </intent-filter>
            </receiver>
    		
    

      

      

      

  • 相关阅读:
    Java生鲜电商平台-微服务架构利弊分析(生鲜小程序/APP)
    实用---eclipse中的代码提示功能
    实验---数据结构中字符串的匹配问题
    实验---实现两个指数递减多项式的和与积
    实验----Java的二维数组的应用及杨辉三角的编写
    你也想成为白帽子么?推荐一份网络安全书单!
    互联网公司的敏捷开发是怎么回事?这一份软件工程书单送给你!
    曾经我也有一个做游戏的梦想,这几本游戏开发的书籍推荐给为未来的游戏工程师
    如何做好Linux服务器运维,你可能需要这一份运维工程师书单!
    如何做好Linux服务器运维,你可能需要这一份运维工程师书单!
  • 原文地址:https://www.cnblogs.com/crushgirl/p/15241575.html
Copyright © 2011-2022 走看看