1

I am creating an app locker. I want to detect when the user launches another application so i can show the user my lock screen. Right now I can open lock screen against a specific action i.e. when the user ons the screen. When the status of the screen changes my service runs and checks the foreground app. If that app is in the list of app i have blocked then the lock screen appears. But i want to start service everytime the user launches another app. Following is the code of my BroadcastReciever class

  public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_SCREEN_ON.equals(intent.getAction()))
{

    Intent service = new Intent(context, BackgroundService.class);
    context.startService(service);
    
    Intent service1 = new Intent(context, BackgroundService.class);
    context.stopService(service1);
}
  • check this link for more information about your question: https://stackoverflow.com/questions/3290936/android-detect-when-other-apps-are-launched and this one: https://android.jlelse.eu/how-to-detect-android-application-open-and-close-background-and-foreground-events-1b4713784b57 – Mustafa Poya Nov 05 '20 at 07:00

1 Answers1

1

you cannot detect if an app is launched at a particular time ,but what you can certainly find out if any other app is in foreground.

 public static boolean isForeground(Context ctx, String myPackage){
     ActivityManager manager = (ActivityManager) 
     ctx.getSystemService(ACTIVITY_SERVICE);
      List< ActivityManager.RunningTaskInfo > runningTaskInfo = 
      manager.getRunningTasks(1); 

  ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
  if(componentInfo.getPackageName().equals(myPackage)) {
    return true;
 }       
 return false;
}

To detect apps if they are just in the RAM , either in foreground or background, use this : ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();

 for (int i = 0; i < runningAppProcessInfo.size(); i++) {
 if(runningAppProcessInfo.get(i).processName.equals("com.the.app.you.are.looking.for") 
{
// Do you stuff
  }
}

You can probabaly consider inititating BG check and trynna find out, if no. apps in foreground has increased by on which will means that a new app was launched.

Option 2:

I think we can use logcat and analyze it's output.

In all similar programs I have found this permission :

 android.permission.READ_LOGS

It means all of them use it but it seems the program starts and after that our program (app protector) will start and bring front.

Use below code :

try
    {
        Process mLogcatProc = null;
        BufferedReader reader = null;
         mLogcatProc = Runtime.getRuntime().exec(new String[]{"logcat", "-d"});

        reader = new BufferedReader(new 
    InputStreamReader(mLogcatProc.getInputStream()));

    String line;
    final StringBuilder log = new StringBuilder();
    String separator = System.getProperty("line.separator"); 

    while ((line = reader.readLine()) != null)
    {
        log.append(line);
        log.append(separator);
    }
    String w = log.toString();
    Toast.makeText(getApplicationContext(),w, Toast.LENGTH_LONG).show();
}
catch (Exception e) 
{
    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}

Notes :-Third part applications cant use the systen permission to read log for security reasons aboove Android 4.1 Read more here, https://commonsware.com/blog/2012/07/12/read-logs-regression.html.Option 1 is your best bet.

Rishabh Ritweek
  • 550
  • 3
  • 10
  • I am getting the apps which are in the foreground. e.g when I open my screen from the sleep mode. It checks which app is opened at the moment. I need to know how can I know when the user launches an app so I can show the lock screen. There are many apps like this on the playstore. So , I would like to know how they work – Usama Shakeel Nov 05 '20 at 07:12
  • Somebody had the same question.. i found out this in quora https://www.quora.com/How-do-I-detect-if-an-android-app-is-launched..so, i will research about this specifically if you insist on it – Rishabh Ritweek Nov 05 '20 at 07:14
  • I have a suggestion.. you can constantly check in a bg thread if no, of apps available in the foreground has increased by one,that means a new app was launched – Rishabh Ritweek Nov 05 '20 at 07:22
  • I would really appreciate any help that I can get at the moment as I have been working on it for the past 3 days and have not been able to find a solution – Usama Shakeel Nov 05 '20 at 07:22
  • give me some time..meanwhile you can also look at my last comment. – Rishabh Ritweek Nov 05 '20 at 07:23
  • Yeah I am going to try it. Appreciate your help :) – Usama Shakeel Nov 05 '20 at 07:26
  • Can u plz elaborate the option2. How am I going to read log and where should I put this code?? – Usama Shakeel Nov 05 '20 at 07:32
  • I also think that this method does not work anymore – Usama Shakeel Nov 05 '20 at 07:39
  • Toast is just the process that will launch.this is just these messages that you see when you aunch the app but if ound out that from Android 4.1, they are not allowing third part apps to use this permission..so, option 1 is your best bet..you can try option 2 but most likely, its not gonna work – Rishabh Ritweek Nov 05 '20 at 07:39
  • Option1 will work,, ..just try it out..if i found something more, i wil update it. – Rishabh Ritweek Nov 05 '20 at 07:40
  • Can u plz also clarify option 1. Where should I create the background thread. Should I create it in my Service class?? My service class gets triggered at the start of my app and also when I perform a specific action like SCREEN_ON – Usama Shakeel Nov 05 '20 at 07:49
  • yes, you can do so in OnStartCommand, assuming that you start using startService().use callbacks to activity by asking your activty to implement an inteface which will have a function to receive a callback, Ofcourse, there are other ways for your activity to receive callbacks like explicit broadcast.serach in google about that – Rishabh Ritweek Nov 05 '20 at 07:53