I am using following line of code to get the package name of top running application in Android
ActivityManager am = (ActivityManager) context.getSystemService(Activity.ACTIVITY_SERVICE);
String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();
What I want is the label name of top running activity in the stack, like if facebook is running on the top , I should get "facebook" as label name of the application
public String getTopActivityStackName()
{
ActivityManager mActivityManager = (ActivityManager)
getSystemService(Activity.ACTIVITY_SERVICE);
PackageManager mPackageManager = getPackageManager();
String packageName = mActivityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
ApplicationInfo mApplicationInfo;
try
{
mApplicationInfo = mPackageManager.getApplicationInfo( packageName, 0);
} catch (NameNotFoundException e) {
mApplicationInfo = null;
}
String appName = (String) (mApplicationInfo != null ?
mPackageManager.getApplicationLabel(mApplicationInfo) : "(unknown)");
return appName;
}
try this i dont know whether it help you or not ,
PackageManager pm = context.getPackageManager();
RunningAppProcessInfo info = am.getRunningAppProcesses().get(0);
try {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA)); System.out.println("the label of the app is " + c); Log.w("LABEL", c.toString());
}
catch(Exception e) { //Name Not FOund Exception
}
Related
I am writing a code to extract the package name, application name, and icon from the last installed app on my phone. I can get the application common name and icon from the application info, but I can't seem to figure out how to get the package name. All the codes that I have found to get the package name give me the package name of MY app, not the last installed app.
It seems like I need to find a method to get the package name, where I can pass in the application info as the parameter (like I do for the application common name and icon).
final PackageManager pm = context.getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo(intent.getData().getSchemeSpecificPart(), 0);
Log.d("tag_name","Application Info" + ai);
PACKAGE_NAME = context.getApplicationContext().getPackageName();
Log.d("tag_name","Package Name" + PACKAGE_NAME);
} catch (final PackageManager.NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
Log.d("tag_name", "Application NAME" + applicationName);
// http://www.carbonrider.com/2016/01/01/extract-app-icon-in-android/
try {
Drawable icon = context.getPackageManager().getApplicationIcon(ai);
Log.d("tag_name", "ICON" + icon);}
catch (Exception e){}
Firstly receive all apps with this Code :
List<PackageInfo> packages = packageManager.getInstalledPackages(PackageManager.GET_META_DATA);
Then sort the packages list with this code :
Collections.sort(packages, new Comparator<PackageInfo>() {
#Override
public int compare(PackageInfo p1, PackageInfo p2) {
return Long.toString(p2.firstInstallTime).compareTo(Long.toString(p1.firstInstallTime));
}
});
Then you can receive the package Name of the latest installed app this way:
packages.get(0).packageName
I'm looking for a way to retrieve an activity's title in Android Lollipop.
What I mean is not the process name or even the app's name, but the title that is given to the activity when it is open. For example, when you navigate to http://www.google.com/ in Chrome, the title changes from "Chrome" to "Google", which can be seen when you hit the recent apps button, and I need something that returns the word "Google".
Below is my awful attempt at looping through the currently running apps and trying to find that information.
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses();
for (int i = 0; i < runningAppProcessInfo.size(); i++) {
PackageManager pm = context.getPackageManager();
ApplicationInfo info = null;
try {
info = pm.getApplicationInfo(runningAppProcessInfo.get(i).processName, 0);
Log.i("DEBUG_LABEL", "Process Name: " + runningAppProcessInfo.get(i).processName);
Log.i("DEBUG_LABEL", "App Label: " + pm.getApplicationLabel(info).toString());
if (info.className != null) {
Log.i("DEBUG_LABEL", "Info's package and class name: " + info.packageName + ", " + info.className);
ComponentName cName = new ComponentName(info.packageName, info.className);
ActivityInfo activityInfo = pm.getActivityInfo(cName, PackageManager.GET_META_DATA);
Log.i("DEBUG_LABEL", "Activity Label: " + activityInfo.loadLabel(pm));
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
Apparently what I was looking for was the description of the Task.
List<ActivityManager.RunningTaskInfo> rTaskInfoList = am.getRunningTasks(100);
...
return rTaskInfoList.get(i).description.toString() // Returns "Google", instead of "Chrome"
I am trying to read AndroidManifest file to extract all android permissions in eclipse plugin developnebt but have reached a dead end. It is not able to open file in /Project_root_directory/res/AndroidManifest.xml. I searched and found that it is in encrypted form.
I can't figure how to read it using Java. Any help is appreciated.
private void getInstalledApps()
{
PackageManager pkgMgr = c.getPackageManager();
List<ApplicationInfo> list = pkgMgr.getInstalledApplications(0);
for (ApplicationInfo content : list)
{
String pname = content.packageName;
String appLabel = (String) pkgMgr.getApplicationLabel(content);
PackageInfo pi = null;
try
{
pi = pkgMgr.getPackageInfo( pname,
PackageManager.GET_META_DATA |
PackageManager.GET_PERMISSIONS );
String[] reqPerms = pi.requestedPermissions;
}
catch ( NameNotFoundException e1 )
{
if (D) e1.printStackTrace();
}
}
}
I have to determine list of permission used each by the installed application on my device.
I have got the list of applications installed and there package name using the following code:
PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> list = m.queryIntentActivities(intent,PackageManager.PERMISSION_GRANTED);
for (ResolveInfo rInfo : list) {
Log.d("Installed Applications", rInfo.activityInfo.applicationInfo
.loadLabel(pm).toString());
Log.d("packegename",rInfo.activityInfo.applicationInfo.packageName.
toString());
}
How do I get permission used by each application?
So i coded it.i needed not just the permissions but also the recievers and services.pls see the following code,hope its useful for others.
PackageManager p = this.getPackageManager();
final List <PackageInfo> appinstall=p.getInstalledPackages(PackageManager.GET_PERMISSIONS|PackageManager.GET_RECEIVERS|
PackageManager.GET_SERVICES|PackageManager.GET_PROVIDERS);
for(PackageInfo pInfo:appinstall){
//PermissionInfo[] permission=pInfo.permissions;
String[] reqPermission=pInfo.requestedPermissions;
ServiceInfo[] services=pInfo.services;
ProviderInfo[] providers=pInfo.providers;
int versionCode=pInfo.versionCode;
Log.d("versionCode-package ",Integer.toString(versionCode));
Log.d("Installed Applications", pInfo.applicationInfo
.loadLabel(pm).toString());
Log.d("packegename",pInfo.packageName.
toString());
if(reqPermission!=null)
for(int i=0;i<reqPermission.length;i++)
Log.d("permission list",reqPermission[i]);
}
NOTICE-setting flags is important otherwise it causes problem n u cnt get services ,provider
NOTE- NULL CHECK IS IMP OR IT GIVES NPE
also the previous code i wrote ws using activityInfo this one uses packageInfo .it better now i guess :)
happy coding ppl :)
Here how to retrieve the list of the apps installed on an Android device, and the permissions used by every app.
private static final String TAG = "MyActivity";
...
final PackageManager pm = getPackageManager();
final List<ApplicationInfo> installedApps = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for ( ApplicationInfo app : installedApps ) {
//Details:
Log.d(TAG, "Package: " + app.packageName);
Log.d(TAG, "UID: " + app.uid);
Log.d(TAG, "Directory: " + app.sourceDir);
//Permissions:
StringBuffer permissions = new StringBuffer();
try {
PackageInfo packageInfo = pm.getPackageInfo(app.packageName, PackageManager.GET_PERMISSIONS);
String[] requestedPermissions = packageInfo.requestedPermissions;
if ( requestedPermissions != null ) {
for (int i = 0; i < requestedPermissions.length; i++) {
permissions.append(requestedPermissions[i] + "\n");
}
Log.d(TAG, "Permissions: " + permissions);
}
}
catch ( PackageManager.NameNotFoundException e ) {
e.printStackTrace();
}
}
Check out freetaskmanager
// Loop through all installed packaged to get a list of used permissions and PackageInfos
for (PackageInfo pi : appList) {
// 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>();
try {
PermissionInfo pinfo = mPm.getPermissionInfo(permission, PackageManager.GET_META_DATA);
CharSequence label = pinfo.loadLabel(mPm);
CharSequence desc = pinfo.loadDescription(mPm);
} catch (NameNotFoundException e) {
Log.i(TAG, "Ignoring unknown permission " + permission);
continue;
}
}
}
This is the only thing I found, though I've not tested it out.
Let me know if it works for any one:
pm.getPackageInfo(rInfo.activityInfo.applicationInfo.packageName, packageManager.GET_PERMISSIONS);
I am trying to make a program that shows all the active applications.
I searched everywhere but could only find code that shows the package name only.
It would be of great help if you masters can tell me how to display all the active application name
Did you try using ActivityManager.getRunningAppProcesses()?
Here is the sample code for retrieving names: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));
Log.w("LABEL", c.toString());
}catch(Exception e) {
//Name Not FOund Exception
}
}
If you are getting the package name, you should be able to get additional information about the application using the PackageManager:
http://developer.android.com/reference/android/content/pm/PackageManager.html
There are direct methods for getting the application icon, ApplicationInfo and ActivityInfo objects. Off the top of my head I don't know which one would direct you to the readable name, but if its not directly accessible through one of the methods here, it should be accessible from the application resources (also accessible from this class).
If you are writing a service and want to update the current "foreground app" at regular intervals like I did, DO NOT be tempted to get the ActivityManager instance in the onCreate() of your service and re-use it when updating the current app name:
public class MyService extends Service implements {
ActivityManager mActivityManager;
#Override public void onCreate() {
mActivityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE ); }
String getForegroundAppName() {
String appname;
List <RunningAppProcessInfo> l;
l = mActivityManager.getRunningAppProcesses();
while( i.hasNext() ) {
if ( info.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && !isRunningService(info.processName) {
currentApp = info;
break;
}
}
if ( currentApp != null ) {
try {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(currentApp.processName, PackageManager.GET_META_DATA ));
appname = c.toString();
}
return appname;
}
}
Don't do this. It causes a memory leak resulting in numerous GC_CONCURRENT errors every time it's called. I don't know the real reason behind this but it's much cleaner to get the ApplicationManager instance each time you use it like this:
public class MyService extends Service implements {
#Override public void onCreate() {... }
String getForegroundAppName() {
ActivityManager mActivityManager;
String appname;
mActivityManager = (ActivityManager)getSystemService(ACTIVITY_SERVICE );
List <RunningAppProcessInfo> l;
l = mActivityManager.getRunningAppProcesses();
while( i.hasNext() ) {
if ( info.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && !isRunningService(info.processName) {
currentApp = info;
break;
}
}
if ( currentApp != null ) {
try {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(currentApp.processName, PackageManager.GET_META_DATA ));
appname = c.toString();
}
return appname;
}
}