检测其他应用何时启动:Android服务中的无限循环

检测其他应用何时启动:Android服务中的无限循环,android,android-intent,service,infinite-loop,battery,Android,Android Intent,Service,Infinite Loop,Battery,我需要实现应用程序保护功能,用户可以阻止几个应用程序,并在启动任何被阻止的应用程序时显示输入密码屏幕。为此,我正在运行一项服务,该服务将在其他应用程序启动时检测。当至少有一个应用程序被阻止时,我将启动下面的服务,当没有应用程序被阻止时,我将停止服务。并在设备启动时启动服务。下面是我这样做的代码。但是设备的电池消耗得太快了。你能告诉我有什么更好的方法来满足我的要求吗。请引导我 public class OtherAppLaunchDetectService extends Service impl

我需要实现应用程序保护功能,用户可以阻止几个应用程序,并在启动任何被阻止的应用程序时显示输入密码屏幕。为此,我正在运行一项服务,该服务将在其他应用程序启动时检测。当至少有一个应用程序被阻止时,我将启动下面的服务,当没有应用程序被阻止时,我将停止服务。并在设备启动时启动服务。下面是我这样做的代码。但是设备的电池消耗得太快了。你能告诉我有什么更好的方法来满足我的要求吗。请引导我

public class OtherAppLaunchDetectService extends Service implements Runnable{

private Thread thread = null;
private boolean isRunning = false;
private ActivityManager am; 
private ArrayList<String> currentlyBlockedAppsList;
private PackageManager packageManager;
private Context currentContext;
private ArrayList<String> homeScreenAppsList;

public void setRunning(boolean isRunning) {
    this.isRunning = isRunning;
}

private void startThread(){
    setRunning(true);
    thread = new Thread(this);
    thread.start();
}

private void stopThread(){
    setRunning(false);
}

@Override
public void onCreate() {
    am = ((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE));
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onDestroy() {
    super.onDestroy();
    stopThread();
 }

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    currentContext = getApplicationContext();
    packageManager = currentContext.getPackageManager();
    homeScreenAppsList = getHomeScreenApps();
    stopThread();
    thread = new Thread(this);
    startThread();
    return START_STICKY; 
} 

public void run(){
    while(isRunning){
            try {
                if(am == null){
                    am = ((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE));
                }
                String currentRunningAppPKGName = am.getRunningTasks(1).get(0).topActivity.getPackageName();

                if(currentContext == null){
                    currentContext = getApplicationContext();
                }
                if(homeScreenAppsList == null){
                    homeScreenAppsList = getHomeScreenApps();
                }
                if(!currentRunningAppPKGName.equals(currentContext.getApplicationInfo().packageName) && !homeScreenAppsList.contains(currentRunningAppPKGName)){
                    System.out.println("current Running PKG Name : "+currentRunningAppPKGName);

                    currentlyBlockedAppsList = getCurrentlyBlockedAppsListFromSqliteDB();
                    if(currentlyBlockedAppsList != null && !currentlyBlockedAppsList.isEmpty() && currentlyBlockedAppsList.contains(currentRunningAppPKGName)){
                        if(currentContext == null){
                            currentContext = getApplicationContext();
                        }
                        Utils.launchEnterPassCodeScreen(currentContext);
                    }
                }
            }catch (Exception e) {
                e.printStackTrace(); 
            } 
        }
}


private ArrayList<String> getCurrentlyBlockedAppsListFromSqliteDB() { 
  //returns the list of apps packages which are blocked currently 
}

private ArrayList<String> getAllInstalledAppsList() {
    ArrayList<String> allAppsList = new ArrayList<String>();

    final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

    final List<ResolveInfo> pkgAppsList = getApplicationContext().getPackageManager().queryIntentActivities(mainIntent, 0);
    for (ResolveInfo res : pkgAppsList) {
        allAppsList.add(res.activityInfo.packageName);
    }
    return allAppsList;
} 

private ArrayList<String> getHomeScreenApps() {
    ArrayList<String> allAppsList = new ArrayList<String>();

    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);

    final List<ResolveInfo> pkgAppsList = getApplicationContext().getPackageManager().queryIntentActivities(startMain, 0);
    for (ResolveInfo res : pkgAppsList) {
        allAppsList.add(res.activityInfo.packageName);
    }
    return allAppsList;
}
}
公共类OtherAppLaunchDetectService扩展服务实现Runnable{
私有线程线程=null;
私有布尔值isRunning=false;
私人活动经理am;
私有ArrayList currentlyBlockedAppsList;
专用软件包管理器软件包管理器;
私人语境;
私人ArrayList homeScreenAppsList;
公共void setRunning(布尔值isRunning){
this.isRunning=isRunning;
}
私有void startThread(){
设置正在运行(真);
线程=新线程(此);
thread.start();
}
私有void stopThread(){
setRunning(假);
}
@凌驾
public void onCreate(){
am=((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE));
}
@凌驾
公共IBinder onBind(意向){
返回null;
}
@凌驾
公共空间{
super.ondestory();
止动螺纹();
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
currentContext=getApplicationContext();
packageManager=currentContext.getPackageManager();
homeScreenAppsList=getHomeScreenApps();
止动螺纹();
线程=新线程(此);
startThread();
返回开始时间;
} 
公开募捐{
同时(正在运行){
试一试{
如果(am==null){
am=((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE));
}
字符串currentRunningAppPKGName=am.getRunningTasks(1.get(0.topActivity.getPackageName();
如果(currentContext==null){
currentContext=getApplicationContext();
}
if(homeScreenAppsList==null){
homeScreenAppsList=getHomeScreenApps();
}
如果(!currentRunningAppPKGName.equals(currentContext.getApplicationInfo().packageName)和&!homeScreenAppsList.contains(currentRunningAppPKGName)){
System.out.println(“当前运行包名称:”+currentRunningAppPKGName);
currentlyBlockedAppsList=getCurrentlyBlockedAppsListFromSqliteDB();
如果(currentlyBlockedAppsList!=null&&!currentlyBlockedAppsList.isEmpty()&¤tlyBlockedAppsList.contains(currentRunningAppPKGName)){
如果(currentContext==null){
currentContext=getApplicationContext();
}
Utils.LaunchInterpassCodeScreen(currentContext);
}
}
}捕获(例外e){
e、 printStackTrace();
} 
}
}
私有ArrayList GetCurrentlyBlockedAppListFromSqliteDB(){
//返回当前被阻止的应用程序包列表
}
私有ArrayList GetAllInstalledAppList(){
ArrayList allAppsList=新建ArrayList();
最终意图maintent=新意图(Intent.ACTION_MAIN,空);
mainIntent.addCategory(Intent.CATEGORY_启动器);
最终列表pkgAppsList=getApplicationContext().getPackageManager().QueryInputActivities(mainIntent,0);
对于(ResolveInfo res:pkgAppsList){
allAppsList.add(res.activityInfo.packageName);
}
返回列表;
} 
private ArrayList getHomeScreenApps(){
ArrayList allAppsList=新建ArrayList();
Intent startMain=新意图(Intent.ACTION\u MAIN);
startMain.addCategory(Intent.CATEGORY\u HOME);
最终列表pkgAppsList=getApplicationContext().getPackageManager().QueryInputActivities(startMain,0);
对于(ResolveInfo res:pkgAppsList){
allAppsList.add(res.activityInfo.packageName);
}
返回列表;
}
}

你能解决你的问题吗?