Use a service to start an Activity when another App is launched

483 views Asked by At

I'm trying to make a service which opens up an activity or some kind of window, when it detects that a new App - which is not on the whitelist - was launched. This way I want to prevent people from getting to an App like the Browser, which they cant acces via my Custom-launcher, but with Apps they can acces from the launcher (eg. a link in an App which they should be allowed to use).

My code works fine, if the App that shouldnt be started is itself (my customlauncher), but if theres another App not on the whitelist and i launch it it wont do anything.

public class AppLockerService extends Service {
public AppLockerService() {
}



@Override
public int onStartCommand(Intent intent, int flags, int startId) {


    final List whitelist = Arrays.asList(R.array.WhitelistedApps);


    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {


            ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
            List<ActivityManager.RunningAppProcessInfo> appProcesses = am.getRunningAppProcesses();
            for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
                try {
                    if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                        String openedApp = (String) appProcess.pkgList[0];
                        if (whitelist.contains(openedApp)) {
                            Intent launchIntent = new Intent(AppLockerService.this, ForbiddenApp.class);
                            launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(launchIntent);
                        }
                    }

                } catch (Exception e) {
                }

            }







        }
    }, 0, 1000);
    return START_STICKY;
}





@Override
public void onCreate() {
    // The service is being created
}


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

@Override
public void onDestroy() {
    super.onDestroy();
    Intent intent = new Intent(this, AppLockerService.class);
    startService(intent);
}


}

I would guess, that the bug has something to do with the Intent not being correct, but i dont know how it has to be. i also tried to open an completely other app instead of forbiddenapp.class but it still only works if the bad App ist the App to which the service belongs.

Thanks a lot in advance!

PS.: completely different aproaches are welcome too!

0

There are 0 answers