Get all app in android phone - android

I want to get list of all the apps installed in android phone. By following code I am able to get only user installed apps but I want to get system apps too.
packageManager = getPackageManager();
List<PackageInfo> packageList = packageManager.getInstalledPackages(packageManager.GET_PERMISSIONS);//PackageManager.GET_META_DATA
List<PackageInfo> packageList1 = new ArrayList<PackageInfo>();
PackageManager pm = this.getPackageManager();
/*To filter out System apps*/
for(PackageInfo pi : packageList) {
boolean b = isSystemPackage(pi);
if(!b) {
packageList1.add(pi);
}
}
apkList = (ListView) findViewById(R.id.AppList);
apkList.setAdapter(new ApkAdapter(this, packageList1, packageManager));
apkList.setOnItemClickListener(this);
}
private boolean isSystemPackage(PackageInfo pkgInfo) {
return ((pkgInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) ? true
: false;
}

Try this:
final PackageManager pm = getActivity().getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> apps = pm.queryIntentActivities(intent, PackageManager.GET_META_DATA);
Hope it will helps you..!

Related

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

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.

Get list of installed applications that can be launched

I have a custom listPreference I would like to display a list of apps that can be launched (contain an activity with CATEGORY_LAUNCHER). The selection will be used later to launch the application. When I did a search for the solution, the list also contained apps that could not be launched. Is there any way to narrow this down?
public class AppSelectorPreference extends ListPreference {
#Override
public int findIndexOfValue(String value) {
return 0;
//return super.findIndexOfValue(value);
}
public AppSelectorPreference(Context context, AttributeSet attrs) {
super(context,attrs);
PackageManager pm = context.getPackageManager();
List<PackageInfo> appListInfo = pm.getInstalledPackages(0);
CharSequence[] entries = new CharSequence[appListInfo.size()];
CharSequence[] entryValues = new CharSequence[appListInfo.size()];
try {
int i = 0;
for (PackageInfo p : appListInfo) {
if (p.applicationInfo.uid > 10000) {
entries[i] = p.applicationInfo.loadLabel(pm).toString();
entryValues[i] = p.applicationInfo.packageName.toString();
i++;
}
}
} catch (Exception e) {
e.printStackTrace();
}
setEntries(entries);
setEntryValues(entryValues);
}
}
Solved:
final Context context = getBaseContext();
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List<ResolveInfo> pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
CharSequence[] entries = new CharSequence[pkgAppsList.size()];
CharSequence[] entryValues = new CharSequence[pkgAppsList.size()];
int i = 0;
for ( ResolveInfo P : pkgAppsList ) {
entryValues[i] = (CharSequence) P.getClass().getName();
entries[i] = P.loadLabel(context.getPackageManager());
++i;
};
#Frazerm63 i think you are missing this thing in Your Code
Intent localIntent = new Intent("android.intent.action.MAIN", null);
localIntent.addCategory("android.intent.category.LAUNCHER");
List localList = localPackageManager.queryIntentActivities(localIntent, 0);
Collections.sort(localList, new ResolveInfo.DisplayNameComparator(localPackageManager));
you have to pass your PackageManager object in above code .means this localPackageManager
i have not much idea how you can use this in user Code but this will help to get you idea to filter only some category 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();
}

Categories

Resources