get Installed apps from package name - android

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

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 application names with specific permission

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

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 display list of application names from package names

I am developing an application which show all current applications. I am getting the apps package names, thats good but not specific applications name How should I do it, I am using following code:
Context context = this.getApplicationContext();
ActivityManager mgr = (ActivityManager)context.getSystemService(ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = mgr.getRunningTasks(30);
List<ActivityManager.RunningAppProcessInfo> tasksP = mgr.getRunningAppProcesses();
int numOfTasks = tasks.size();
for(int i = 0; i < numOfTasks; i++){
ActivityManager.RunningAppProcessInfo task = tasksP.get(i);
PackageInfo myPInfo = null;
try {
myPInfo = getPackageManager().getPackageInfo(task.processName, 0);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
Toast.makeText(location.this,
task.processName,
Toast.LENGTH_LONG).show();
}
Also please tell me how to display these apps name in check boxes, so that I can kill my desired app.
Try this code for getting Installed Application in your phone :
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List<ResolveInfo> pkgAppsList = this.getPackageManager().queryIntentActivities( mainIntent, 0);
List<String> componentList = new ArrayList<String>();
for (ResolveInfo ri : pkgAppsList) {
if (ri.activityInfo != null) {
String app_name=ri.activityInfo.loadLabel(getPackageManager()).toString();
Log.d("Apps Name", ""+ri.activityInfo.loadLabel(getPackageManager()).toString());
}
}
}
Check this Link for further Reference :
Installed Application Details
Intent filter = new Intent(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_LAUNCHER);
Context context = getApplicationContext();
PackageManager manager = getPackageManager();
List<ResolveInfo> infos = getPackageManager().queryIntentActivities(filter,0);
List<Intent> filters = new ArrayList<Intent>();
filters.add(filter);
ComponentName component = new ComponentName(context.getPackageName(), MainActivity.class.getName());
List<ComponentName> activities = new ArrayList<ComponentName>();
//ComponentName[] components = new ComponentName[] {new ComponentName("//com.neo.application", "com.neo.application.Application"), component};
try {
manager.getApplicationInfo(//PACKAGE_NAME);//just Ctrl+space it u'll get the package names.
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

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