1

I need to implement app protector feature in which the user can block few apps and presented by a enter pass code screen when he starts any of blocked apps. For that I'm running a service which will detect the when other app is getting launched. I'm starting the below service when atleast one app is blocked and stopping when no app is blocked. And starting the service on device boot up too. Below is my code to do so. But the Device Battery is draining so fast. Could you please tell me any better way to run the service for my requirement. Please guide me.

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;
}
}
user1670443
  • 461
  • 2
  • 6
  • 17

0 Answers0