Android Launcher: get the list of apps installed on your mobile phone. Android launcher

Source: Internet
Author: User

Android Launcher: get the list of apps installed on your mobile phone. Android launcher

The most important of Launcher is to obtain the entries and icons of all application lists. Generally, there are two methods to obtain them:

PackageInfo

ResolveInfo

Run and obtain the Launcher of all apps and allow click events to go to the application.



The following two methods are used to obtain the list of all applications:

Create basic data:

PakageMod. java

public class PakageMod {

	public String pakageName;
	public String appName;
	public Drawable icon;

	public PakageMod() {
		super();
	}
	
	public PakageMod(String pakageName, String appName, Drawable icon) {
		super();
		this.pakageName = pakageName;
		this.appName = appName;
		this.icon = icon;
	}
}



Create an adapter:


Public class DemoGridAdapter extends BaseAdapter {

Private LayoutInflater inflater;
Private List < PakageMod > datas;

Public DemoGridAdapter(Context, List datas) {</pakagemod>
Super ();
Inflater = LayoutInflater. The from (context);
This. Datas = datas;
}

@ Override
Public int getCount () {
Return datas. The size ();
}

@ Override
Public Object getItem(int position) {
Return null;
}

@ Override
Public long getItemId(int position) {
Return the position;
}

@ Override
Public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
If (convertView == null) {
// the object that USES the View itemView is associated with r.layout.item
ConvertView = inflater. Inflate (R.l ayout. Apps, null);
Holder = new ViewHolder ();
Holder. The icon = (ImageView) convertView. The findViewById (da pps_image R.i);
Holder. The label = (TextView) convertView
. The findViewById (da pps_textview R.i);
ConvertView. SetTag (holder);
} else {
Holder = (ViewHolder) convertView. GetTag ();
}

Holder. The icon. SetImageDrawable (datas. The get (position). The icon).
Holder. The label. The setText (datas. The get (position). AppName);

Return convertView.

}

The class ViewHolder {
Private ImageView icon;
Private TextView label;
}
}




Create an adapter View:


<!--?</span--> The XML version = "1.0" encoding = "utf-8"? >
The < LinearLayout XMLNS: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: gravity = "center"
Android: orientation = "vertical" >

The < ImageView
The android: id = "@ + id/apps_image"
Android: layout_width = "48 dip"
48 dip "android: layout_height ="
The android: icon = "@ drawable/ic_launcher" / >

The < TextView
The android: id = "@ + id/apps_textview"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: maxEms = "5"
Android: maxLines = "1"
Android: text = "good" / >

< / LinearLayout >

The list is obtained in the Activity and displayed in the GridView. Click to enter the application;


Use PackageInfo


Public class PackageInfoDemo extends Activity {

Private GridView GridView.
Private PackageManager pManager;
Private List < PakageMod > datas;
Private String tag = "MainActivity";

@ Override
Protected void onCreate(Bundle savedInstanceState) {
Super. OnCreate (savedInstanceState);
Enclosing the setContentView (R.l ayout. Activity_main);
/ / get the gridview
The gridview = (the gridview) the findViewById (R.i d.g ridview);
// get image, application name, package name
PManager = PackageInfoDemo. This. GetPackageManager ();
A List < PackageInfo > = appList getAllApps (PackageInfoDemo. This);
Datas = new ArrayList < PakageMod > ();
For (int I = 0; I < appList. The size (); I++) {
PackageInfo pinfo = appList. Get (I);
PakageMod shareItem = new PakageMod();
// set the picture
ShareItem. Icon = pManager. GetApplicationIcon (pinfo. ApplicationInfo);
// sets the application name
ShareItem. AppName = pManager. GetApplicationLabel (
Pinfo. ApplicationInfo). The toString ();
// sets the package name of the application
ShareItem. PakageName = pinfo. ApplicationInfo. PackageName;

Datas. The add (shareItem);

}
The gridview. SetAdapter (new baseAdapter (this, datas));

// respond when you click the apply icon
The gridview. SetOnItemClickListener (new ClickListener ());
}

Public static List getAllApps(Context) {</packageinfo>

List apps = new ArrayList();</packageinfo></packageinfo>
PackageManager pManager = context. GetPackageManager ();
// get all the apps in the phone
A List < PackageInfo > packlist = pManager. GetInstalledPackages (0);
For (int I = 0; I < packlist. The size (); I++) {
PackageInfo pak = (PackageInfo) packlist.get(I);
// if() if the value is <=0, it is your own program; otherwise, it comes with system engineering
If ((pak. ApplicationInfo. Flags & pak. ApplicationInfo. FLAG_SYSTEM) < = 0) {
// add your own installed applications
/ / apps. The add (pak);
}
Apps. The add (pak);
}
Return apps;
}

Private class ClickListener implements OnItemClickListener {

@ Override
Public void onItemClick (AdapterView <!--?</span--> > arg0, View arg1, int position,
Long arg3) {
Intent Intent = new Intent();
Intent = PackageInfoDemo. This. GetPackageManager ()
GetLaunchIntentForPackage (datas. Get (position). PakageName);
StartActivity (intent);
}
}
}

Use ResolveInfo


Public class ResolveInfoDemo extends Activity {
Private GridView GridView.
Private PackageManager pManager;
Private List < PakageMod > datas;
Private String tag = "ResolveInfoDemo";

@ Override
Protected void onCreate(Bundle savedInstanceState) {
// TODO auto-generated method stub
Super. OnCreate (savedInstanceState);
Enclosing the setContentView (R.l ayout. Activity_main);
/ / get the gridview
The gridview = (the gridview) the findViewById (R.i d.g ridview);

// get image, application name, package name
PManager = this. GetPackageManager ();

Datas = new GetAllApps (this). GetDatas ();
The gridview. SetAdapter (new DemoGridAdapter (this, datas));
The gridview. SetOnItemClickListener (new ClickListener ());
}

// this class responds when the user clicks on the application icon
Private class ClickListener implements OnItemClickListener {

@ Override
Public void onItemClick (AdapterView <!--?</span--> > adapterView, View View, int arg2,
Long arg3) {
Intent Intent = new Intent();
Intent = getPackageManager (). GetLaunchIntentForPackage (
Datas. The get (arg2). PakageName);
StartActivity (intent);
}

}
}

GetAllApps. java


public class GetAllApps {

	private Context mContext;
	private PackageManager packageManager;
	private int mIconDpi;
	private String tag = "GetAllApps";
	private List<PakageMod> datas = new ArrayList<PakageMod>();
	
	public GetAllApps(Context mContext){
		this.mContext = mContext;
		 ActivityManager activityManager =
	                (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
		packageManager = mContext.getPackageManager();
		 mIconDpi = activityManager.getLauncherLargeIconDensity();
	}
	
	public void loadAllAppsByBatch() {
		List<ResolveInfo> apps = null;
		Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
		mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
		apps = packageManager.queryIntentActivities(mainIntent, 0);
		for (int i = 0; i < apps.size(); i++) {
			String packageName = apps.get(i).activityInfo.applicationInfo.packageName;
			String title = apps.get(i).loadLabel(packageManager).toString();
			Drawable icon = null;
			if(title == null){
				title = apps.get(i).activityInfo.name;
			}
			ActivityInfo info = apps.get(i).activityInfo;
			icon = getFullResIcon(info);
			datas.add(new PakageMod(packageName,title,icon));
		}
	}

	public Drawable getFullResIcon(ActivityInfo info) {
        Resources resources;
        try {
            resources = packageManager.getResourcesForApplication(
                    info.applicationInfo);
        } catch (PackageManager.NameNotFoundException e) {
            resources = null;
        }
        if (resources != null) {
            int iconId = info.getIconResource();            
            if (iconId != 0) {
                return getFullResIcon(resources, iconId);
            }
        }
        return getFullResDefaultActivityIcon();
    }
	
	public Drawable getFullResDefaultActivityIcon() {
        return getFullResIcon(Resources.getSystem(),
                android.R.mipmap.sym_def_app_icon);
    }
	
	public Drawable getFullResIcon(Resources resources, int iconId) {
        Drawable d;
        try {
        	// requires API level 15 (current min is 14):
            d = resources.getDrawableForDensity(iconId, mIconDpi);
        } catch (Resources.NotFoundException e) {
            d = null;
        }

        return (d != null) ? d : getFullResDefaultActivityIcon();
    }

	public List<PakageMod> getDatas() {
		loadAllAppsByBatch();
		return datas;
	}
}

Here, getDrawableForDensity must be supported by sdks of version 15 or later. Launcher of all earlier versions cannot be used;


You do not need to configure any permissions for the two methods used in this blog;


In the Launcher of Android4.2, ResolveInfo is used to obtain the list of applications used. The ResolveInfo Demo here is also copied from the Launcher source code;

There may be 3rd methods... To be continued;

This article is from the CSDN blog. For more information, contact the author;
Indicate source http://blog.csdn.net/dreamintheworld/article/details/39718581






How does android launcher refresh the list of installed applications?

There is a column in the code for you to see

 
Android gets the list of installed applications

Hulu Xia !!!!!!
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.