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
  • 550
  • 3
  • 8
  • 21

3 Answers3

0

Yes, it's possible in Android. You can check whether third party application is in forground or is in background using below code

   class ForegroundCheckTask extends AsyncTask<Context, Void, Boolean> {

  @Override
  protected Boolean doInBackground(Context... params) {
    final Context context = params[0].getApplicationContext();
    return isAppOnForeground(context);
  }

  private boolean isAppOnForeground(Context context) {
    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
    if (appProcesses == null) {
      return false;
    }
    final String packageName = context.getPackageName();
    for (RunningAppProcessInfo appProcess : appProcesses) {
      if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
        return true;
      }
    }
    return false;
  }
}
Rahul Arora
  • 21
  • 1
  • 4
  • How, using this code, instantly realized that the application is starting? – user1854307 Jun 07 '17 at 10:12
  • You want to know whether particular application is starting or not like you want to know whether facebook is starting or not Right ? – Rahul Arora Jun 07 '17 at 10:23
  • Yes, I need my application to perform certain actions at the time of launching other applications. – user1854307 Jun 07 '17 at 10:48
  • ok, got it. You need to pass package name of that application(third party application) in above code and above code will return you whether application is in forground or not.If it is in forground then you can perform action. – Rahul Arora Jun 07 '17 at 10:57
  • You need to start service in background for this and use above code in service. service will run continuously on background and will check whether application is in foreground, when you will get application is in foreground it means they launched. I did similar thing in past. – Rahul Arora Jun 07 '17 at 11:04
  • Applied your code, but appProcesses always stores the name of my package, although in the foreground the application is third-party. – user1854307 Jun 07 '17 at 12:31
  • final String packageName = context.getPackageName(); In this line you need to add third party application package name like i am adding facebook package name because context.getPackageName() use for getting own application package name final String packageName = "com.facebook.katana"; – Rahul Arora Jun 07 '17 at 13:22
  • I delete packageName in my realization for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { appIsForeground = appProcess.processName; return appIsForeground; } } – user1854307 Jun 07 '17 at 13:46
  • No. In collection appProcesses contains name only package my app. Although in the foreground application is third-party. – user1854307 Jun 07 '17 at 14:08
  • There are two way to use above code 1. You can add package name manually(static) 2. You can get package name of installed(third party) applications via code.. – Rahul Arora Jun 07 '17 at 14:13
  • So here is the code final PackageManager pm = getPackageManager(); //get a list of installed apps. List packages = pm.getInstalledApplications(PackageManager.GET_META_DATA); for (ApplicationInfo packageInfo : packages) { Log.d(TAG, "Installed package :" + packageInfo.packageName); Log.d(TAG, "Source dir : " + packageInfo.sourceDir); Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName)); } – Rahul Arora Jun 07 '17 at 14:14
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
  • 24
  • 4
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
  • 10,596
  • 2
  • 29
  • 39
  • 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