Get an Activity's Title in Android - android

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"

Related

Android: How to show app picker to user if he wants to use navigation intent?

Now I have special Intent for GoogleMaps only in my App. But some of the users don't have GoogleMaps installed on their device. I don't want to limit user to use only Google Maps app. Is there any way to show some picker which will show all installed navigation apps in his phone to choose from?
Now I have only GoogleMaps intent:
Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?"+(map.lastLocation?.run { "saddr=$latitude,$longitude&" }?:"")+"daddr="+dst.run { "$latitude,$longitude&travel_mode=$mapMode" }))
i'm not sure if it helps or not, but you can get the list all apps which use location using below code:
PackageManager pm = getPackageManager();
List packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo applicationInfo : packages) {
Log.d("test", "App: " + applicationInfo.name + " Package: " +
applicationInfo.packageName);
try {
PackageInfo packageInfo = pm.getPackageInfo(applicationInfo.packageName, PackageManager.GET_PERMISSIONS);
//Get Permissions
String[] requestedPermissions = packageInfo.requestedPermissions;
if(requestedPermissions != null) {
for (int i = 0; i < requestedPermissions.length; i++) {
Log.d("test", requestedPermissions[i]);
//////////////////////////////////////
//////////////////////////////////////
// Look for the desired permission here
//////////////////////////////////////
//////////////////////////////////////
}
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
and add following line to your manifest file:
<uses-permission android:name="android.permission.GET_TASKS"/>

Extracting the package name of the last installed app on my phone

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

How can I see the application directly underneath a soft keyboard in Android?

I am developing a soft keyboard that pipes data (not text) to a receiving intent. Is there any way to see the application/activity that the keyboard is currently overlaying? I am trying to preselect the intent action chooser and develop custom rules to get the data to go to the right place.
Just thought I'd answer my own question for this one. Here is the code that returns the activity from the top of the stack.
public String PullTop() {
final PackageManager pm = getPackageManager();
//Get the Activity Manager Object
ActivityManager aManager =
(ActivityManager) self.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);
Log.i("Package",pkgInfo.packageName);
return pkgInfo.packageName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
Log.d("", "NameNotFoundException :" + rapInfo.pkgList[0]);
}
}
return null;
}
To pipe the data to the right process just check for null and:
intent.setPackage(PullTop());

Get label name of top running activity in the stack Android

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
}

Determine list of permissions used by an installed application in Android

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);

Categories

Resources