Get Current Running Process in Android - android

It's about Currently Running Process Info.
I would like to capture the vivid name or the same name which appeared under icon of the current running process.
While using RunningTaskInfo or RunningProcessInfo , I can only get the complex information like com.android.browser.
Actually, I want to get the names like Brower, Clock, Contact etc.
How can I achieve this? Please direct to the full tutorial link or with fully explanation codes.
PS: I am just a beginner and I am sure that I've already explored the Android Developer site.

Get the current RunningAppProcessInfo from pid
public static String getCurrentProcessName(Context context) {
// Log.d(TAG, "getCurrentProcessName");
int pid = android.os.Process.myPid();
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses())
{
// Log.d(TAG, processInfo.processName);
if (processInfo.pid == pid)
return processInfo.processName;
}
return "";
}

Once you get the package name (such as com.android.browser), simply use that name with PackageManager:
PackageManager pm = context.getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(name, 0);
CharSequence name = pm.getApplicationLabel(ai);
You'll need to handle some exceptions, but you will get the real app names from this.

Related

Receive other applications' usage time data

I'm working on an android project. I need to receive other applications' usage time. I simply just need how many application is running and what are they. How can I access these kinds of data?
I found some articles about receiving another's SQLite DB but I really don't need this.
You may follow the steps:
Retrieve the list of the tasks that are currently running by calling
getRunningTasks()
Go through each of the task and retrieve the PackageName
Get AppName from the PackagName
Add the AppName in a list
Here is the implementation:
private List<String> getRunningAppList()
{
Set<String> appNameList = new HashSet<>();
PackageManager pm = getPackageManager();
ActivityManager aManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
// Get the list of the tasks that are currently running
List<ActivityManager.RunningTaskInfo> infoList = aManager.getRunningTasks(Integer.MAX_VALUE);
for (ActivityManager.RunningTaskInfo taskInfo : infoList)
{
// Get the packageNAme
String pkgName = taskInfo.topActivity.getPackageName();
String appName = null;
// Get the appName
try
{
appName = pm.getApplicationLabel(pm.getApplicationInfo(pkgName,PackageManager.GET_META_DATA)).toString();
} catch (PackageManager.NameNotFoundException e)
{
e.printStackTrace();
}
// Adding the appName
appNameList.add(appName);
}
return new ArrayList<>(appNameList);
}
Using UsageStats, UsageStatsManager is simply the best approach for this topic.
https://developer.android.com/reference/android/app/usage/UsageStats
UsageStats.getLastTimeUsed() informs you about a specific application's last usage time by date format.

Retrieving Java package name of AIDL consumer

I am exposing some API through AIDL mechanism. Clients need to bind to the AIDL and call methods synchronously. Is there a way I can retrieve the client's Java package name?
For example, if I expose a method boolean isFooAvailable() as AIDL API, from within the implementation of isFooAvalable, can I determine the Java package name of the app that binds to the AIDL service?
Expanding on the comment given by CommonsWare, inside your Service you can use
PackageManager packageManager = context.getPackageManager();
String callingName = packageManager.getNameForUid(Binder.getCallingUid());
If you're trying to restrict access to of callers you should also check the certificates of the calling app.
PackageInfo callingPackageInfo = packageManager
.getPackageInfo(callingPackage, PackageManager.GET_SIGNATURES);
// Make sure to check there is only one signature, or you may run into a security
// vulnerability: https://androidvulnerabilities.org/vulnerabilities/Fake_ID
verify(callingPackageInfo.signatures);
Yes, you can find out the package name from within the implementation as follows :
IAidl.Stub mBinder = new IAidl.Stub() {
#Override
public boolean isFooAvailable() throws RemoteException {
String pkgName = "";
int pid = Binder.getCallingPid();
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> processes = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo proc : processes) {
if (proc.pid == pid) {
pkgName = proc.processName;
}
}
// package name of calling application package
Log.e("Package Name", pkgName);
return false;
}
}
Finding package name through PID is the best approach compared to UID.

How can I determine permission of Running apps programmatically in android?

I want to see the permission of running apps of android in my software.
For this reason ,I have the following code :
List<App> apps = new ArrayList<App>();
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
PackageManager packageManager = getPackageManager();
List<RunningAppProcessInfo> l = am.getRunningAppProcesses();
Iterator<RunningAppProcessInfo> i = l.iterator();
PackageManager pm = this.getPackageManager();
int row_count = 0 ;
while(i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try
{
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
App app = new App();
app.setTitle(c.toString());
app.setPackageName(l.get(row_count).processName);
PackageInfo packageInfo = packageManager.getPackageInfo(l.get(row_count).processName, PackageManager.GET_PERMISSIONS);
String[] reqPermission= packageInfo.requestedPermissions;
app.set_Permission_Info(reqPermission);
// app.setVersionName(p.versionName);
// app.setVersionCode(p.versionCode);
// CharSequence description = p.applicationInfo.loadDescription(packageManager);
// app.setDescription(description != null ? description.toString() : "");
row_count++;
// app.setSize(p.s)
apps.add(app);
}
catch(Exception e){}
But there is a problem.
When I run my apps ,I find that the app name and app's package name are not consistent . Why has this problem introduced?
The main problem is described follow:
Let us suppose an apps named "EBOOK_Reader" and "Camera" is running in my device . The package name is "com.a.b" and "com.c.d" respectively. The problem of this code is the appropriate package name is not with appropriate apps name .
It shows the package name Of "com.a.b" to "Camera " and "com.c.d" to "EBOOK_Reader" which is not desired .
Any idea of how can the problem be solved?
ThankYou
This is correct and Running:
PackageManager mPm = getPackageManager();
List <PackageInfo> appList=mPm.getInstalledPackages(PackageManager.GET_PERMISSIONS|PackageManager.GET_RECEIVERS|
PackageManager.GET_SERVICES|PackageManager.GET_PROVIDERS);
for (PackageInfo pi : appList) {
System.out.println("Process Name: "+pi);
// Do not add System Packages
if ((pi.requestedPermissions == null || pi.packageName.equals("android")) ||
(pi.applicationInfo != null && (pi.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0))
continue;
for (String permission : pi.requestedPermissions) {
//Map<String, String> curChildMap = new HashMap<String, String>();
//System.out.println("############ "+permission);
try {
PermissionInfo pinfo = mPm.getPermissionInfo(permission, PackageManager.GET_META_DATA);
CharSequence label = pinfo.loadLabel(mPm);
CharSequence desc = pinfo.loadDescription(mPm);
System.out.println("$$$$$ "+label+"!!!!!! "+desc);
} catch (NameNotFoundException e) {
Log.i(TAG, "Ignoring unknown permission " + permission);
continue;
}
}
}
The app name and app's package name are normally different. You better use the package name as this is unique throughout the device.
Update:
Now I understand your problem. Thanks for clarifying. It is because of the variable row_count. Basically you're are using two different iterator variables. That's why your getting 2 different results. You don't need row_count because you already have interator for i.
Try the updated code below:
Basically l.get(row_count).processName was replaced by info.processName.
List<App> apps = new ArrayList<App>();
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
PackageManager packageManager = getPackageManager();
List<RunningAppProcessInfo> l = am.getRunningAppProcesses();
Iterator<RunningAppProcessInfo> i = l.iterator();
PackageManager pm = this.getPackageManager();
// int row_count = 0 ; // no need for this. feel free to delete
while(i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try
{
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
App app = new App();
app.setTitle(c.toString());
app.setPackageName(info.processName);
PackageInfo packageInfo = packageManager.getPackageInfo(info.processName, PackageManager.GET_PERMISSIONS);
String[] reqPermission= packageInfo.requestedPermissions;
app.set_Permission_Info(reqPermission);
// app.setVersionName(p.versionName);
// app.setVersionCode(p.versionCode);
// CharSequence description = p.applicationInfo.loadDescription(packageManager);
// app.setDescription(description != null ? description.toString() : "");
//row_count++; // no need for this. feel free to delete
// app.setSize(p.s)
apps.add(app);
}
catch(Exception e){}
Process names are not bound to the application package name. They happen to be the same by default, as a convenience. However, each app is free to change its process name in its manifest using the android:process attribute, or to spawn more processes with different names for various components.
And in even more advanced scenarios, multiple applications can share the same process.
In particular, what this means is you can't use the process name to get the application(s) that are running currently. You should instead iterate over the list of packages that are loaded in that process using the RunningAppProcessInfo.pkgList field instead. Keep in mind that it is an array, and can contain more than one application package name. (See the note about the advanced scenarios above)
On a separate note, as the documentation for the getRunningAppProcesses() states:
Note: this method is only intended for debugging or building a user-facing process management UI.

How to profile the App uses in Android?

I want to collect App activation sequence in an Android system.
E.g.
If a user first open Youtube App, then switch to Gmail App, then switch back to Youtube App and so on, then the sequence is like:
Youtube Gmail Youtube ...
Is there available App existing in Google Play or somewhere else to achieve this?
Is it straightforward to implement? Is it possible to achieve the goal with pure App solution?
Is it require rooting the device?
You need to poll this function on some regular interval.
You can further write a logic to remove subsequently duplicate Application Label.
private string getActiveApplicationLabel(){
String appLabel = null;
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while(i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName,
PackageManager.GET_META_DATA));
appLabel = c.toString();
}catch(Exception e) {
//Name Not FOund Exception
}
}
return appLabel;
}

Any way to check if a running process is a system process in android

I am building an android app in which I need to show the list of currently running apps but It contain all the process including many system or defualt process of android like : launcher,dailer etc.
Now is there any way to check if the currently running process is not a system process (default process) of android.
Thanks a lot.
Here is my code:
First to get a list of running Apps do the following
public void RunningApps() {
final PackageManager pm = getPackageManager();
//Get the Activity Manager Object
ActivityManager aManager =
(ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
//Get the list of running Applications
List<ActivityManager.RunningAppProcessInfo> rapInfoList =
aManager.getRunningAppProcesses();
//Iterate all running apps to get their details
for (ActivityManager.RunningAppProcessInfo rapInfo : rapInfoList) {
//error getting package name for this process so move on
if (rapInfo.pkgList.length == 0)
continue;
try {
PackageInfo pkgInfo = pm.getPackageInfo(rapInfo.pkgList[0],
PackageManager.GET_ACTIVITIES);
if (isSystemPackage(pkgInfo)) {
//do something here
}
else {
//do something here
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
Log.d(TAG, "NameNotFoundException :" + rapInfo.pkgList[0]);
}
}
}
The actual function to check if the running application is system app, and used in the previous method is given below
/**
* Return whether the given PackgeInfo represents a system package or not.
* User-installed packages (Market or otherwise) should not be denoted as
* system packages.
*
* #param pkgInfo The Package info object
* #return Boolean value indicating if the application is system app
*/
private boolean isSystemPackage(PackageInfo pkgInfo) {
return (pkgInfo.applicationInfo.flags &
ApplicationInfo.FLAG_SYSTEM) != 0;
}
I hope it helps.
As per this documentation, it seems you can use FLAG_SYSTEM_PROCESS to identify a process is System process or not. Here is SO discussion on this.
You can use ActivtyManager to get list of all running process in android.See the below link for more information Android process killer .

Categories

Resources