Android application info based on it's process name - android

I'm trying to find some info about apps I find using shell's top command. All I have is a process name (containing package name). Icon and app name would be perfect. I can't find any suitable soultion via google. Any help would be aprreciated;)
To be preemptive, I use top because it's the only way I found to show current processor usage. If someone's familiar with some more API friendly soultion, I'd be grateful.
Example process names I get are:
com.android.deskclock for desktop clock
com.creativemobile.DragRacing for game Drag Racing

Here how you get details. Since you have the package name, you can use it to get the corresponding application name, version, and icon.
List<PackageInfo> packagess = getPackageManager().getInstalledPackages(0);
for(int i=0;i<packagess.size();i++) {
PackageInfo pack = packagess.get(i);
if ((!getSysPackages) && (pack.versionName == null)) {
continue ;
}
//this is the application name
pack.applicationInfo.loadLabel(getPackageManager()).toString();
//this is the package name
pack.packageName;
//this is the version name
pack.versionName;
//this is the version code
pack.versionCode;
//this is the application icon
pack.applicationInfo.loadIcon(getPackageManager());
}

You can try out below code :
private String getTopActivity() {
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager.getRunningTasks(1);
ActivityManager.RunningTaskInfo ar = RunningTask.get(30);
return ar.topActivity.getClassName().toString();
}
you have to give the permission of get task in the AndroidManifest.xml

Related

Why Launcher Activity's package name is not listed in Recent Tasks list in Android 9

navigating multiple applications from launcher, in Recent Tasks list contains recent used application's package name but we could not able to see Launcher application's package name in Android 9. Please help me
Here is my code for getting recent tasks list:
ActivityManager am = ((ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE));
List<ActivityManager.RecentTaskInfo> taskInfo = am.getRecentTasks(2,ActivityManager.RECENT_WITH_EXCLUDED);
for(int i=0;i<taskInfo.size();i++)
{
Log.i(TAG,"Test"+taskInfo.get(i).baseIntent.getComponent().getPackageName());
}

How to stop opening default apps in android?

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.

Is process name same as package name in android?

By process I mean what we provide in android:process and by package I mean package in
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.osg.appkiller"
android:versionCode="1"
android:versionName="1.0" >
More details Processes and Threads - Android Developer
I wanted to get application names of all running apps. So this is what I did after looking at various sources (and it works).
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
PackageManager packageManager = getPackageManager();
final List<RunningAppProcessInfo> runningProcesses = activityManager.getRunningAppProcesses();
for(RunningAppProcessInfo processInfo : runningProcesses) {
CharSequence appName = null;
try {
appName = packageManager.getApplicationLabel(packageManager.getApplicationInfo(processInfo.processName, PackageManager.GET_META_DATA));
} catch (NameNotFoundException e) {
Log.e(TAG,"Application info not found for process : " + processInfo.processName,e);
}
}
If you see Documentation for PackageManager.getApplicationInfo
ApplicationInfo android.content.pm.PackageManager.getApplicationInfo(String packageName, int flags) throws NameNotFoundException
but I am passing
processInfo.processName
where processName is name of process running. So we are basically using process name as package name to get application info.
First of all is this approach correct ?
Secondly is it true that if we do not provide process for activities/services etc new process is created with same name as that of package name ?
By default android takes the package name as the process name. But if you define process property in application tag in manifest file android:process="com.example.newprocessname" then the application will run with this name "com.example.newprocessname".
As for your questions,
1-> In this case your application name is same as the default package name, that's why it is working. Try changing the process name. It'll not work.
2-> That's true. It is by default. Refer to "android:process" in the following link: https://developer.android.com/guide/topics/manifest/application-element.html
Hope this answers your question!

Kill another Background Application in android

Problem: I want to kill a background application process.
We call the below methods inside a background thread/services and it's not working.
We have tried a few methods available on net but not succeeding to kill the background process/application.
My Device has a root permisision already.
Code here
1st Method:
int value = findPIDbyPackageName("com.google.android.youtube");
android.os.Process.sendSignal(value, 9);
2nd Method:
ActivityManager activityManager = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
activityManager.killBackgroundProcesses("com.google.android.youtube");
3rd Method:
ActivityManager activityManager = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
activityManager.restartPackage("com.google.android.youtube");
4th Method :
android.os.Process.killProcess(pid);
5th Method : `
List<ApplicationInfo> packages;
PackageManager pm;
pm = getPackageManager();
packages = pm.getInstalledApplications(0);
ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
for (ApplicationInfo packageInfo : packages) {
if((packageInfo.flags & ApplicationInfo.FLAG_SYSTEM)==1)continue;
if(packageInfo.packageName.equals("mypackage")) continue; // here my package defines your application package entered in manifest
else if(packageInfo.packageName.equals("third Party application Package Name")) // if you dont have this package name then prefer playstore url of this app to get packagename
mActivityManager.killBackgroundProcesses(packageInfo.packageName);
}
6th
We launched a third party app like youtube, Subway surf from our own android app.
We are using startActivityForResultmethod for launching the app.
launchApp("com.imangi.templerun");
protected void launchApp(String packageName) {
mIntent = getPackageManager().getLaunchIntentForPackage(packageName);
if (mIntent != null) {
try {
startActivityForResult(mIntent, 101);
} catch (ActivityNotFoundException err) {
Toast t = Toast.makeText(this, "App not found", Toast.LENGTH_SHORT); t.show();
}
}
}
To close the application, the method finsihActivity(ResposneCode) is available.
But we're not able to use it in service.
You are not allowed to kill processes which don't belong to your app. The system will decide when to kill and what to kill when necessary.
App intervention is just to tell the system a message, Please help me to kill this process, blah blah..., that's all.
Process.killProcess() should work, this is your method number 4.
Read carefully what it says.
Kill the process with the given PID. Note that, though this API allows
us to request to kill any process based on its PID, the kernel will
still impose standard restrictions on which PIDs you are actually able
to kill. Typically this means only the process running the caller's
packages/application and any additional processes created by that app;
packages sharing a common UID will also be able to kill each other's
processes.
Under root permission you should be able to remove the kernel restrictions, unfortunately I do not know how to do this, I suggest you to search for native solution for that, may be some C codes, there are plenty of them in the net.
Also in your method 1 you been trying to send SIGNAL_KILL, try sending SIGNAL_QUIT

Is there a way to get Process Ids of other 3rd party applications in Android...?

I have a requirement in my project where in i have to kill process of 3rd party application.
As i know Android can have multiple application running at a time, so can i get all those Process
Ids some how...
This class will help you to kill processes:
http://developer.android.com/reference/android/os/Process.html
This gives you a list of currently running processes:
ActivityManager.getRunningAppProcesses();
The list contains ActivityManager.RunningAppProcessInfo objects which store the pid. You can then kill the processes via
Process.killProcess(pid);
You need the proper permissions to do that. Android should throw an exception if you try to kill a process without the proper permissions and tell you what permission you need.
check this way if it solves your purpose:
ArrayList<PackageInfo> res = new ArrayList<PackageInfo>();
PackageManager pm = context.getApplicationContext().getPackageManager();
List<PackageInfo> packs = pm.getInstalledPackages(0);
packs.get(pos).gids;
packs.get(pos).sharedUserId;
You can get information about all running processes using ActivityManager. Refer following code:
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<RunningAppProcessInfo> list = am.getRunningAppProcesses();
for (RunningAppProcessInfo proc : list) {
Log.v("Proccess", proc.processName + " : " + proc.pid);
}
Multiple applications can be running in the same process, so it would be better to use RunninAppProcessInfo.pkgList over RunninAppProcessInfo.processName.

Categories

Resources