Android app listing permissions only - android

I am developing an android application that will display all the applications installed in the mobile phone and when clicking on a particular application, it should show only permissions of that application. I used package manager to fetch the details of all the details of the apk's installed.
The part of the code is here:
PackageManager packageManager = context.getPackageManager();
List<PackageInfo> applist = packageManager.getInstalledPackages(0);
Iterator<PackageInfo> it = applist.iterator();
while (it.hasNext()) {
PackageInfo pk = (PackageInfo) it.next();
PackageInfo pk1 = (PackageInfo) it.next();
if ((pk.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
// Log.v("System app using internet = ",""+ pk.applicationInfo.loadLabel(packageManager));
String p = pk.applicationInfo.loadLabel(packageManager).toString();
if (PackageManager.PERMISSION_GRANTED == packageManager.checkPermission(Manifest.permission.CAMERA, pk.packageName))
//results.add("" +"\n"+pk.applicationInfo.loadLabel(packageManager));
{
//Drawable appicon = getPackageManager().getApplicationIcon("com.google.maps");
results.add("" + "\n" + pk.applicationInfo.loadLabel(packageManager));
}
if (PackageManager.PERMISSION_GRANTED == packageManager.checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, pk.packageName))
results1.add("" +"\n"+pk1.applicationInfo.loadLabel(packageManager));
}
}
I should get the icons followed by the application name while listing the applications.
Icon and application name example
How should I modify this code so that the required output is got.

Example app
Official document
core code:
packageInfo.requestedPermissions?.forEach { permission -> ... }
More links
https://developer.android.com/reference/android/Manifest.permission.html
https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/res/AndroidManifest.xml
http://androidpermissions.com/

Related

How to get version any app in phone

I want to check any app version in phone ( android studio )
and if the last version equal the version install in users phones app
How to get any app version installed on the phone by Android Studio?
You need to iterate all application installed the app, then get each of the package name to get the package info.. in packageInfo have infomation about those app
final PackageManager packageManager = getPackageManager();
List<ApplicationInfo> installedApplications = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
List<String> vers = new ArrayList<>();
for (ApplicationInfo appInfo : installedApplications)
{
PackageInfo packageInfo = getPackageManager().getPackageInfo(appInfo.packageName, 0);
vers.add(packageInfo.versionName);
}
Log.i(TAG,vers.toString());
Get all install app info:
List<PackageInfo> packList = getPackageManager().getInstalledPackages(0);
for (int i=0; i < packList.size(); i++)
{
PackageInfo packInfo = packList.get(i);
if ( (packInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0)
{
String appName = packInfo.applicationInfo.loadLabel(getPackageManager()).toString();
;
Log.e("App " + Integer.toString(i), appName +" ver: "+packInfo.versionCode);
}
}
More info:Info

Android : List all process which uses certain resources

I am new to android programming.
I want to list all the process which uses/access certain resources like :
File(s)
Internet etc
Any sample code or suggestion.
Thanks in Advance ...:)
You will get all application name which is using internet,
private ArrayList<String> getInstalledAppsWithSpecificPermission(Context context) {
ArrayList<String> appsPkgName = new ArrayList<String>();
PackageManager pm = context.getPackageManager();
List<PackageInfo> appNamelist = pm.getInstalledPackages(0);
Iterator<PackageInfo> it = appNamelist.iterator();
while (it.hasNext()) {
PackageInfo pk = (PackageInfo) it.next();
if ((pk.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
Log.d("System Application which using internet = ", ""+pk.applicationInfo.loadLabel(pm));
continue;
}
if (PackageManager.PERMISSION_GRANTED == pm
.checkPermission(Manifest.permission.INTERNET,
pk.packageName))
results.add("" + pk.applicationInfo.loadLabel(pm));
}
Log.v("Application using internet = ", appsPkgName.toString());
return results;
}

Android getInstalledPackages

I am creating an android app which has the list of all the apps which user can access from its android phone menu.
Here is my code which is working successfully..
List<App> apps = new ArrayList<App>();
// the package manager contains the information about all installed apps
PackageManager packageManager = getPackageManager();
List<PackageInfo> packs = packageManager.getInstalledPackages(0); //PackageManager.GET_META_DATA
for(int i=0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
ApplicationInfo a = p.applicationInfo;
// skip system apps if they shall not be included
if ((!includeSysApps) && ((a.flags & ApplicationInfo.FLAG_SYSTEM) == 1)) {
continue;
}
App app = new App();
app.setTitle(p.applicationInfo.loadLabel(packageManager).toString());
app.setPackageName(p.packageName);
app.setVersionName(p.versionName);
app.setVersionCode(p.versionCode);
CharSequence description = p.applicationInfo.loadDescription(packageManager);
app.setDescription(description != null ? description.toString() : "");
apps.add(app);
}
Now this gives me a big list and i can classify its items in 3 ways :
1st (Apps like): Speed Moto, Subway Surf, Chrome (Which i installed)
2nd (Apps like):Camera, Email, Messaging (Installed by Default)
3rd (.... Like):PageBuddyNotiSvc, Dialer Storage etc (Some Packages)
Now i want to filter the 3rd type of Apps and want to keep only 1st and 2nd type..
How can i achieve this list..
You can get the information about each application using the following code
ApplicationInfo app = context.getPackageManager().getApplicationInfo(packageName, 0);
//And then check the dir
if (app.sourceDir.startsWith("/data/app/")) {
//Non-system app
}
else {
//System app
}

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.

Installed application is third-party or not

How can I get the list of installed third-party applications on Android phone.
I am able to get the list of application with the code below but I want only third-party applications.
PackageManager pm = context.getPackageManager();
appInstalModel.setAppName(p.applicationInfo.loadLabel(context.getPackageManager()).toString());
appInstalModel.setAppPkg(p.packageName);
appInstalModel.setAppVersionName(p.versionName);
List<ApplicationInfo> apps = getPackageManager().getInstalledApplications(0);
for (int i=0; i < apps.size(); i++)
{
if ((apps.get(i).flags & ApplicationInfo.FLAG_SYSTEM) == 1)
{
//System app
}
}
RoflcoptrException's answer is correct. But in some cases it won't give you all the installed third-party applications. ApplicationInfo also has flag FLAG_UPDATED_SYSTEM_APP which is set
If this application has been install as an update to a built-in system
application
On my smart phone such applications include Amazone Kindle, Adobe Reader, Slacker Radio and others. These applications did not come with the phone and were installed from Google Play Store. Thus, they can be considered as third-party apps.
So, you may also want to check FLAG_UPDATED_SYSTEM_APP flag.
final PackageManager packageManager = _context.getPackageManager();
List<ApplicationInfo> installedApplications =
packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo appInfo : installedApplications)
{
if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0)
{
// IS A SYSTEM APP
}
if ((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0)
{
// APP WAS INSTALL AS AN UPDATE TO A BUILD-IN SYSTEM APP
}
}
The ApplicationInfo object will have the FLAG_SYSTEM flag unset. The sdmove program might have some sample code.
small changes in #Roflcoptr answer.
List<ApplicationInfo> apps = getPackageManager().getInstalledApplications(0);
for (int i=0; i < apps.size(); i++)
{
if ((apps.get(i).applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1)
{
//System app
}
}
Thanks #Roflcoptr for your answer.
public static List<PackageInfo> getInstalledAppList(Context context) {
ArrayList<PackageInfo> packList = (ArrayList<PackageInfo>) context.getPackageManager().getInstalledPackages(0);
showLog("/n/n ********************** App List ********************");
for (int i = 0; i < packList.size(); i++) {
PackageInfo packInfo = packList.get(i);
if ((packInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
String appName = packInfo.applicationInfo.loadLabel(context.getPackageManager()).toString();
showLog(appName + "(" + packInfo.packageName + ")");
} else {
packList.remove(i);
i--;
}
}
showLog("List Size : " + packList.size());
showLog("/n/n ********************** END ********************");
return packList;
}

Categories

Resources