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 :)
Related
I'm developing a watchdog system service on android.
In case I detect that some foreground app is blocked/halted I want to force restart of the app.
I've already tried kill background app, with no success (as i'm trying to kill a foreground app).
also, via exec(adb shell su killall com.package.name) crashes with no permission, as i'm "just" system, not rooted device.
Finally got a way.
You can get the pid name by
private int getAppUid(String packageName){
ActivityManager mActivityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> pidsTask = mActivityManager.getRunningAppProcesses();
for(int i = 0; i < pidsTask.size(); i++) {
if (pidsTask.get(i).processName.equals(packageName)){
return pidsTask.get(i).uid;
}
}
return -1;
}
after that, you can just kill it with
android.os.Process.killProcess(pUID);
works for my android 7 device.
I am trying to make an android app for users in which user can activate Child protection. for example, User opens my app, he can view all the apps he has
(including default ones). Now if the user blocks default message or calls app then everything should be same, just these selected app must not open, even after anyone tap on these applications multiple time. It should look like nothing happening. I don't want to have a privacy PIN code or pattern on that app. I just want to stop the children by opening any app of mobile selected by the user through my application. Is this possible? if yes then any idea about how can i achieve this thing?
This problem is very similar to the one answered here, that will be my source for this answer. I can't try this code, but I hope it can give you a rough idea to make your first try and see yourself if it work or not.
The idea
The basic idea is to create a Background Service that continuously checks if there are open apps. If those apps are on the list the user locked, the show a lock screen. I don't know if it is possible to close an opened app¹ (I hope it's not!), but for your parental control functionality, a lock screen should have the same effect.
Select the apps to lock.
To retrieve from the system all apps that are installed
PackageManager packageManager = getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> appList = packageManager.queryIntentActivities(mainIntent, 0);
Collections.sort(appList, new ResolveInfo.DisplayNameComparator(packageManager));
List<PackageInfo> packs = packageManager.getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
ApplicationInfo a = p.applicationInfo;
/* Uncomment this to exclude system apps
if ((a.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
continue;
} */
appList.add(p.packageName);
}
Now appList is a List<ResolveInfo>, and you can show to the user a ListView to let her choose the apps to be locked. Save these somewhere (SharedPreferences is an idea).
The service
What the service has to do is to:
Check the currently opened app:
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
activityOnTop = ar.topActivity.getClassName();
Check if activityOnTop is in the locked-apps list.
for(String appName : appList) {
if(appName.equals(activityOnTop)) {
// This app is locked!
LockApp(); // Defined later
break;
}
}
If so, start an Intent to your LockScreen activity (that won't allow any action)
private void LockApp() {
Intent lockIntent = new Intent(mContext, LockScreen.class);
lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(lockIntent);
}
I give a lot of things for granted: you have to start a Service somewhere, you have to build a couple of activities, and this process does not avoid the problem of a smart kid that closes the process in background ... But I think this can work as a starting point.
¹ It apparently is possible. Give a look to this answer, an idea is to substitute the code in LockApp() with something like that.
This doesn't change that I think a Lock Screen is more elegant.
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.
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
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.