Get application names with specific permission - android

I want list of all applications installed on the device which use a specific permission like INTERNET.
i am using this code , but not able to retrieve results , please help .
private ArrayList<String> getInstalledApps(Context context) {
ArrayList<String> results = new ArrayList<String>();
PackageManager packageManager = context.getPackageManager();
List<PackageInfo> applist = packageManager.getInstalledPackages(0);
Iterator<PackageInfo> it = applist.iterator();
while (it.hasNext()) {
PackageInfo pk = (PackageInfo) it.next();
if (PackageManager.PERMISSION_GRANTED == packageManager.checkPermission(Manifest.permission.INTERNET, pk.packageName))
results.add("" + pk.applicationInfo.loadLabel(packageManager));
}
for (int i = 0; i <= results.size(); i++) {
Log.v("app using internet = ", results.toString());
}
return results;
}

I tried the code on android 2.2 emulator and it was working fine. Following is the working code:
private ArrayList<String> getInstalledApps(Context context) {
ArrayList<String> results = new ArrayList<String>();
PackageManager packageManager = context.getPackageManager();
List<PackageInfo> applist = packageManager.getInstalledPackages(0);
Iterator<PackageInfo> it = applist.iterator();
while (it.hasNext()) {
PackageInfo pk = (PackageInfo) it.next();
if ((pk.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
Log.v("system app using internet = ", ""+pk.applicationInfo.loadLabel(packageManager));
continue;
}
if (PackageManager.PERMISSION_GRANTED == packageManager
.checkPermission(Manifest.permission.INTERNET,
pk.packageName))
results.add("" + pk.applicationInfo.loadLabel(packageManager));
}
Log.v("app using internet = ", results.toString());
return results;
}

i had the same scenario, i solved it using the following check
PackageInfo p = packs.get(i);
ApplicationInfo a = p.applicationInfo;
if (PackageManager.PERMISSION_GRANTED != pm.checkPermission(Manifest.permission.INTERNET, p.packageName)) {
continue;
}
the packs variable is a array of class that holds information about the application installed and it consists of the objects with info of all the applications currently installed in system

Related

How to Get the package Names of System pre-installed applications in Android

I Already know how to get the package names of applications installed by the user.
But i cannot figure out how to get the package names of applications that are preinstalled or are system-ware like (Samsung Fit, Calender etc.) ??
List<ApplicationInfo> packages = context.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
Log.d(TAG, "Package :" + packageInfo.packageName);
Log.d(TAG, "Launcher Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName));
}
ApplicationInfo is having a packageName field which provides you package name of the application.
You can use below code to get the list of system application PackageInfo,
public static ArrayList<PackageInfo> getSystemPackageInfos(final Activity context) {
ArrayList<PackageInfo> list = new ArrayList<PackageInfo>();
ArrayList<String> packageNames = new ArrayList<String>();
PackageManager pm = context.getPackageManager();
List<PackageInfo> pinfoList = pm.getInstalledPackages(0);
Collections.sort(pinfoList, PackageNameComparator);
for (PackageInfo pinfo : pinfoList) {
packageNames.add(pinfo.packageName);
boolean isSystem = false;
if (((pinfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0)) {
isSystem = false;
} else {
isSystem = true;
}
if (pinfo.applicationInfo.sourceDir.startsWith("/data/app/") && isSystem) {
//Non-system app
isSystem = false;
}
if (!isSystem) {
continue;
}
Bitmap icon = null;
Drawable apkIcon = getApplicationIcon(pinfo.applicationInfo, context);
try {
icon = Bitmap.createBitmap(apkIcon.getIntrinsicWidth(), apkIcon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(icon);
apkIcon.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
apkIcon.draw(canvas);
} catch (ClassCastException e) {
}
String name = pinfo.applicationInfo.loadLabel(pm).toString();
long apkSize = new File(pinfo.applicationInfo.sourceDir).length();
list.add(pinfo);
}
return list;
}

get Installed apps from package name

i have tried the code i pasted below ..but that gives me every application installed but then i need to check each of the app is mine or not by checking with the package name...Is there a code to where i can pass the package name to the phone system so that it gives me all the apps installed based on that package name.
PackageManager manager = getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_DEFAULT);
final List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent, 0);
Collections.sort(apps, new ResolveInfo.DisplayNameComparator(manager));
ArrayList<String> oArrInstalledHomeApps = new ArrayList<String>();
if (apps != null)
{
final int count = apps.size();
for (int i = 0; i < count; i++)
{
ResolveInfo info = apps.get(i);
String strpackageName = info.activityInfo.applicationInfo.packageName;
if(strpackageName.contains ("com.ZZ."))
{
oArrInstalledHomeApps.add(strpackageName);
}
}
return oArrInstalledHomeApps;
}
Theres a cleaner way to do that.
final PackageManager pm = getPackageManager();
ArrayList<String> oArrInstalledHomeApps = new ArrayList<String>();
// get a list of installed apps.
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages)
{
Log.d(TAG, "Installed package :" + packageInfo.packageName);
Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName));
if (packageInfo.packageName.contains("com.ZZ."))
{
oArrInstalledHomeApps.add(packageInfo.packageName);
}
}
return oArrInstalledHomeApps;
Copied shamelessly from this link

How to get the list of installed application from google play store in android?

i want the application list that is installed from google play or from any external storage, but not the pre installed system application.
i just use below code.....
class PInfo {
private String appname = "";
private String pname = "";
private String versionName = "";
private int versionCode = 0;
private Drawable icon;
private void prettyPrint() {
Log.v("", "*** appname = " + appname + " *** \n" + " packagename = " + pname + "\n" + " version name = " + versionName + "\n" + " version code = " + versionCode + " \n\n\n\n");
}
}
private ArrayList<PInfo> getPackages() {
ArrayList<PInfo> apps = getInstalledApps(true); /* false = no system packages */
final int max = apps.size();
int i = 0;
for (i=0; i<max; i++) {
apps.get(i).prettyPrint();
}
Log.v(TAG, "Total APPs = "+i);
return apps;
}
private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
ArrayList<PInfo> res = new ArrayList<PInfo>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(PackageManager.GET_META_DATA);
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
if ((!getSysPackages) && (p.versionName == null)) {
continue ;
}
PInfo newInfo = new PInfo();
newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
res.add(newInfo);
}
return res;
}
but this code gives me information of system application also.....
can anybody know how to get only installed application except the installed system application ?
thanks....
To discover if your application is a system application you can use the following code:
PackageManager pm = getPackageManager();
List<ApplicationInfo> installedApps = pm.getInstalledApplications(0);
for (ApplicationInfo aInfo: installedApps) {
if ((aInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
// system application
} else {
//user application
}
}
If you need to discover if an application is installed from a market you need to use the following method of Package: getInstallerPackageName. It will return the packageName of the application that installed your application.
public class AppList extends Activity {
private ListView lView;
private ArrayList results = new ArrayList();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lView = (ListView) findViewById(R.id.list1);
PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
for (ResolveInfo rInfo : list) {
results.add(rInfo.activityInfo.applicationInfo
.loadLabel(pm).toString());
Log.w("Installed Applications", rInfo.activityInfo.applicationInfo
.loadLabel(pm).toString());
}
lView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, results));
}
}

Checking if Facebook is installed on Android

How can I check that? Do i have to use PackageManager? Thank you :D
I check it this way (check if the returned value is unequal to null):
public static Intent findFacebookClient(Context con)
{
final String[] FacebookApps = {"com.facebook.android", "com.facebook.katana"};
Intent facebookIntent = new Intent();
facebookIntent.setType("text/plain");
final PackageManager packageManager = con.getPackageManager();
List<ResolveInfo> list = packageManager.queryIntentActivities(facebookIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (int i = 0; i < FacebookApps.length; i++)
{
for (ResolveInfo resolveInfo : list)
{
String p = resolveInfo.activityInfo.packageName;
if (p != null && p.startsWith(FacebookApps[i]))
{
facebookIntent.setPackage(p);
return facebookIntent;
}
}
}
return null;
}
final PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm
.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
Log.d(TAG, "Installed package : " + packageInfo.packageName);
}
Then you can check if packageInfo.packageName is equal to some string which contains the package name of that application.

How to filter nonsystem application from all installed applications

I have the following code and it works well to fetch the details of all applications installed in android but when i use a filter using isSystemPackage(ResolveInfo) it gives force close.
try {
lView = (ListView) findViewById(R.id.list1);
PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
{
List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
for (ResolveInfo rInfo : list) {
if (isSystemPackage(rInfo)) {
results.add(rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
Log.w("Installed Applications", rInfo.activityInfo.applicationInfo
.loadLabel(pm).toString());
}
}
}
} catch(Exception ex) {
Toast.makeText(getApplicationContext(), ex.toString(), 4000).show();
}
it generates force closed
isSystemPackage() code is
private boolean isSystemPackage(ResolveInfo ri){
return ((ri.activityInfo.applicationInfo.flags&ApplicationInfo.FLAG_SYSTEM)!=0)?
true:false;
}
I tried to debug it but can't able to do it.
Got my Answer, This is for others to use this code.
List<ApplicationInfo> list = getPackageManager().getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
for (int n=0;n<list.size();n++) {
if((list.get(n).flags & ApplicationInfo.FLAG_SYSTEM)!=1)
{
results.add(list.get(n).loadLabel(pm).toString());
Log.w("Installed Applications", list.get(n).loadLabel(pm).toString());
}
}
cheers :)
Please try this approach. It works for me:
List<ApplicationInfo> applications = getPackageManager().getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
for (int n=0; n < applications.size(); n++)
{
if ((applications.get(n).flags & ApplicationInfo.FLAG_SYSTEM) != 1)
{
Log.i(tag , "Non-System Aplication name " + applications.get(n).name);
}
}

Categories

Resources