Android, Detect When Other Apps Are Launched

Android, Detect when other apps are launched

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();
}

And do not forget to add it's permission in Manifest file.

Detect When other Application opened or Launched

You cannot detect an App launch in Android. But you can get the list of currently open apps using this code and check if the app you're looking for is open or not:

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 also check if the app is running in the foreground using this method

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;
}

How to detect if another application is open in Flutter?

It is possible at least on Android. I do not program for IOS so I can't say for sure. As @Gaurav pointed out, the solution will need to be custom-made and it probably won't be specific to Flutter. If you are willing to work outside Flutter than it is possible.

Once again, the following solutions are for Android devices:

Solution 1: Since you are targeting social media apps you can lookup the public intents of each app (i.e. Facebook, WhatsApp, etc...) find out the names of the intents they are broadcasting and add a broadcast receiver in your application. Many will say this is not a good idea because the app developer's themselves could change the intents and that you shouldn't listen for anything that hasn't been declared for public use. They are probably right, but this discussion is about how to detect the other apps launching and this method works.

Simply google how to add a broadcast receiver, a good example can be found here.

There are websites that contain databases for a lot of broadcast intents for popular Android applications, but if you have trouble finding the name of a particular one then I'd suggest using ADB to find it. For example, Facebook would be:

adb shell pm dump com.facebook.katana | grep ' filter' | cut -d ' ' -f 12

In some versions of Android broadcast intents do not work, however registering it in the application has worked pretty well for myself.

Once you have a listener setup properly, then you can detect the app when the user launches it and process it accordingly.

Solution 2: You could monitor the processes on the Android device via "Process" and/or "PackageManager" and see when one of the social media applications popup. This method is not very reliable because apps are on all the time and just because they're in the process list doesn't mean they are actively being used.

Solution 3: Once again, this is a very "hacky" solution, but you could listen to the logs on each device. Basically get the output of console logs, read the last 100 lines and see if the app is doing something. If so, then you may be able to determine if the app is active. The biggest problem with this solution (besides trying to read logs on all your user's devices) is that this is not an instant solution and may require special privileges depending on the device you're working on. You would also probably need to setup a service that actively listens and reads the logs, which might cause some significant battery usage.

Best way to detect (any) app (i.e. activity) launch from a my background service

Perhaps my answer in this question might help you. Android how to know an app has been started and range apps priority according the starting times

Place that code inside your background service.

Based on the package name of the activity in the foreground, you can detect that app name by checking all the apps on the phone and matching it with the app that has the same package name.

Edit

Since you want a continuous check, you could use an AsyncTask(), and in the doInBackground() function, just keep getting the list of running activities in each iteration, like I have suggested in that link, and the first item in the taskInfo list will have the activity/app in the foreground. You can also provide another button in your app which on click you stop this , by keeping a bool variable in shared preferences (which you poll for in the while condition in doInBackGround() function of the AsyncTask()).

I have done something similar in the past, and it worked for me.



Related Topics



Leave a reply



Submit