0

Is it possible to track the launch of a third-party application on the device? Maybe the android sends a broadcast when the applications are launched.

UPDATE

public class ServiceAppControl extends IntentService {

private boolean serviceStarted = true;

public ServiceAppControl() {
    super("ServiceAppControl");
}

@Override
protected void onHandleIntent(@Nullable Intent intent) {
    while (serviceStarted){
        String appIsForeground = isAppOnForeground(getBaseContext());
        Log.d(AppGlobal.LOG_TAG, "Запущено приложение " + appIsForeground);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

private String isAppOnForeground(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    String appIsForeground = "";
    if (appProcesses == null) {
        return appIsForeground;
    }
    final String packageName = context.getPackageName();
    for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
        if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
            appIsForeground = appProcess.processName;
            return appIsForeground;
        }
    }
    return appIsForeground;
}

}

user1854307
  • 580
  • 4
  • 8
  • 21

2 Answers2

0

There is no broadcast as such to inform about launch of an application to which others can listen because that can be a security threat. People can use it for malicious purpose. Otherwise from the logs you can get to know about currently launched application.

Priyanka
  • 34
  • 5
0

First, answer your question, Is it possible to track the launch of a third-party application on the device? - YES

Check this link, I explained briefly.

Note: We should be aware of few things before doing few things

  • Running a while loop or timer continuously and getting the recent app will drain a lot of battery(All the AppLocker will do same to get the current opening app) but we should be smart enough and we should run the loop only when the screen ON, when screen OFF we should stop that timer or whatever.

  • From Android "O" some restriction in running the service in background, so better put your service as Foreground service

Done!!!.

Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41
  • How can I monitor the screen on / off? I know that there is a broadcast for this, but it only works when the application is running. If the application is turned off, then the broadcastReceiver does not work. – user1854307 Jun 08 '17 at 11:18
  • We should run the service and you can register the broadcast there. – Muthukrishnan Rajendran Jun 08 '17 at 12:19