how to detect when user launches anapplication on an Android device - android

I am trying to develop an application that detect launch of each application which are installed on device.I want to develop same scenario like this.
Accroding to it logcat supports only below 4.0(ICS) version and my requirement is for above ICS version.I have seen many application on play store likeSmart appLock.How to create this kind of application which supports all version of android from 2.2 onwards.

What I think is that you can keep track of the applications currently running:
final ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < recentTasks.size(); i++)
{
Log.d("Executed app", "Application executed : " +recentTasks.get(i).baseActivity.toShortString()+ "\t\t ID: "+recentTasks.get(i).id+"");
}
Once your polling yealds a new app entry into the list above, you get your desired result.

Related

How to check if my app is still running in an Android service?

I'm starting to develop an Android sticky service for my app with Delphi 10.1 Berlin, but I haven't found any tutorial or book that get into it, so I'm asking:
Which is the simplest way to detect if my app is still running or has been killed by the OS/user?
You can look for the process id by package name and see if it is still active.
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> pids = am.getRunningAppProcesses();
int processid = 0;
for (int i = 0; i < pids.size(); i++) {
ActivityManager.RunningAppProcessInfo info = pids.get(i);
if (info.processName.equalsIgnoreCase("packageNameSearchingFor")) {
processid = info.pid; //found it, we are running
}
}
Or you can simply store a shared value in public shared pref or database accessible through ContentProvider that is updated when in foreground or background to check. Either way is fine.

How to kill tasks in android?

I use the following snippet to kill tasks , it works perfectly fine but it doesn't kill my own app. How do i make it kill my app as well?
// kill tasks
List<String> reservedPackages = new ArrayList<String>();
reservedPackages.add("system");
reservedPackages.add("com.android.launcher2");
reservedPackages.add("com.android.inputmethod.latin");
reservedPackages.add("com.android.phone");
reservedPackages.add("com.android.wallpaper");
reservedPackages.add("com.google.process.gapps");
reservedPackages.add("android.process.acore");
reservedPackages.add("android.process.media");
ActivityManager am = (ActivityManager) context
.getSystemService(Activity.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> listeProcessus = am
.getRunningAppProcesses();
for (RunningAppProcessInfo processus : listeProcessus) {
String packageName = processus.processName.split(":")[0];
if (!context.getPackageName().equals(packageName)
&& !reservedPackages.contains(packageName)) {
am.restartPackage(packageName);
}
}
Google has recently removed the GET_TASKS permission and developers can't make changes to running tasks . The only remained way is to have a root access and close the running tasks that is not possible for All Android Users
As of api 21 (android lolipop) it has been removed by Google

Stop background apps permanently Android

I am curious how apps like Task Managers & a specific app named Purify are able to permanently stop(force stop) background apps without requiring root permissions.
I searched & found a few ways but they were not efficient enough to stop all apps.
First method I found was to use KILLBACKGROUNDPROCESSES function but it was not able to kill all the background apps.
Secondly, I read about the SU FORCE STOP method but it requires root.
How are the apps able to do it without root?
Some Android applications can close other processes that are currently running in the background, not all process can be terminated. Some process even if terminated are restarted immediately. (You can try to close some from the 'application manager' and see for yourself).
You could try the following code that i compiled, it allows you to specify which processes that you may not want to kill.
// List of packages not to kill
List<String> reservedPackages = new ArrayList<String>();
reservedPackages.add("system");
reservedPackages.add("com.android.launcher2");
reservedPackages.add("com.android.inputmethod.latin");
reservedPackages.add("com.android.phone");
reservedPackages.add("com.android.wallpaper");
reservedPackages.add("com.google.process.gapps");
reservedPackages.add("android.process.acore");
reservedPackages.add("android.process.media");
//kill all except that in the list above
int processKillCounter = 0;
ActivityManager am = (ActivityManager) this.getBaseContext().getSystemService(Activity.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> listeProcessus = am.getRunningAppProcesses();
for(ActivityManager.RunningAppProcessInfo processus : listeProcessus) {
String packageName = processus.processName.split(":")[0];
if (!this.getBaseContext().getPackageName().equals(packageName) && !reservedPackages.contains(packageName)) {
am.restartPackage(packageName);
processKillCounter++;
}
}
mi = new ActivityManager.MemoryInfo();
activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
activityManager.getMemoryInfo(mi);
long afterBoostMemory = mi.availMem / 1048576L;
long boostedMemory = afterBoostMemory - availableMegs;
//Percentage can be calculated for API 16+
long percentAvail = mi.availMem / mi.totalMem;
Toast.makeText(this,"Phone Boosted, "+boostedMemory + " MB freed", Toast.LENGTH_LONG).show();

Problems in getting the list of running apps

I need to get the list of running apps(apps that you see when you click on "recent apps" button). Below is the function I'm using to check whether an app running in background
boolean isNamedProcessRunning(String packageName){
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
for (int i = 0; i < runningAppProcessInfo.size(); i++) {
Log.e("", "---> running "+runningAppProcessInfo.get(i).processName);
if(runningAppProcessInfo.get(i).processName.equals(packageName)) {
return true;
}
}
return false;
}
Everything seemed to be fine until I tried "Chrome" app. For instance, I opened chrome and closed by using home button. And I could see "Chrome" on "app history" / "running apps" list. And I swiped away chrome from there. Still the above code says "Chrome" is running on background. How can I solve this?
KillProcess only can kill that process. otherwise it will be listed in getRunningAppProcesses()
Please check this answer
https://stackoverflow.com/a/11865184/3020568

Open and close 3rd party app within my app in android

I know it is impossible to close a running 3rd party app.
But what if i opened the 3rd party app within my app? Is there a way to close/finish it?
Yes it is possible but the user should have that app installed on his device. see following links
Open another application from your own (intent)
Launch an application from another application on Android
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> pids = am.getRunningAppProcesses();
int processid = 0;
for(int i = 0; i < pids.size(); i++)
{
ActivityManager.RunningAppProcessInfo info = pids.get(i);
if(info.processName.equalsIgnoreCase("here your package name")){
processid = info.pid;
}
}
android.os.Process.killProcess(processid);
Hope it will help :)

Categories

Resources