This article describes the Android implementation of the list of software click to start another program function. Share to everyone for your reference, specific as follows:
There is a feature in many of the software currently available: Which software is installed in the device, which they will show to the user in the form of a list of software.
Today we are going to implement this feature:
operating Environment: Motorola defy+ System 2.3.6
main API: Packageinfo,packagemanager,layoutinflater,applicationinfo
Packagemanger class , its primary responsibility is to manage the application package. Through it, we can get the application information
Obtain application-related information through Packagemanager, and then display the appropriate information through ListView.
Directly on the main code.
public class Applistview extends LinearLayout implements onitemclicklistener{private final static String TAG = "Applis
Tview ";
Private ListView Mlistview;
Private TextView Mtvtitle;
Private list<appinfo> mapplist;
Private context Mcontext;
Private Layoutinflater Minflater;
Private Packagemanager Mpacmanager;
Public Applistview (context, AttributeSet attrs) {Super (context, attrs);
Init (context);
Public Applistview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);
Init (context);
Public Applistview {Super (context);
Init (context);
private void init (context c) {mcontext = C;
Minflater = (layoutinflater) c.getsystemservice (Context.layout_inflater_service);
This.addview (Minflater.inflate (R.layout.activity_main,null,false));
Mlistview = (ListView) This.findviewbyid (R.id.listview);
Mtvtitle = (TextView) This.findviewbyid (r.id.title); Loadappdata ();
Mlistview.setadapter (New Myadapter (c));
Mlistview.setonitemclicklistener (this);
LOG.D (TAG, "altogether" +mapplist.size ());
//load application software data software name, package name, corresponding icon, etc. private void loadappdata () {if (mapplist!= null) {mapplist.clear ();
}else{mapplist = new arraylist<appinfo> ();
} Mpacmanager = Mcontext.getpackagemanager ();
list<packageinfo> packages = mpacmanager.getinstalledpackages (0);
for (int i=0; i<packages.size (); i++) {PackageInfo pi = packages.get (i);
AppInfo ai = new AppInfo ();
Ai.packagename = Pi.packagename;
Ai.appname = Pi.applicationInfo.loadLabel (Mpacmanager). toString ();
Ai.appicon = Pi.applicationInfo.loadIcon (Mpacmanager);
Mapplist.add (AI);
Mtvtitle.settext ("Total number of software installed on this machine:" +packages.size () + "");
///For ListView Custom Adapter class Myadapter extends Baseadapter {public myadapter (context c) {mcontext = C; @Override public int GetCount () {
return mapplist = = Null?0:mapplist.size ();
@Override public Object getitem (int arg0) {return mapplist = = Null?null:mapplist.get (arg0);
@Override public long Getitemid (int arg0) {return arg0;
@Override public view GetView (int arg0, view arg1, ViewGroup arg2) {view view;
if (arg1 = = null) {view = Minflater.inflate (R.layout.app_list_item, NULL);
}else{view = arg1;
AppInfo ai = (AppInfo) getitem (arg0);
ImageView AppIcon = (imageview) View.findviewbyid (R.id.appicon);
TextView appName = (TextView) View.findviewbyid (r.id.appname);
TextView apppackage = (TextView) View.findviewbyid (r.id.apppackage);
Appicon.setimagedrawable (Ai.appicon);
Appname.settext (Ai.appname);
Apppackage.settext (Ai.packagename);
return view; }///Handle ListView Item's Click Action, I am here to launch the application @Override public void Onitemclick (adapterview<?> arg0, View arg1, in T arg2, Long Arg3) {Toast.maketext (Mcontext, arg2+ "", Toast.length_short). Show ();
Gets the package name for this item String packname = Mapplist.get (arg2). PackageName;
Start this program Intent Intent = Mpacmanager.getlaunchintentforpackage (packname);
Mcontext.startactivity (Intent);
}//Software carrier public class AppInfo {//software name public String appname= "";
Package name public String packagename= "";
Software icon Public drawable appicon=null;
}
}
Public abstract Intent Getlaunchintentforpackage (String packagename)
This method returns a Intent through the package name and then launches the application via StartActivity (Intent)
Full Instance code click here Download the site .
The following is a diagram of how the program works:
More interested readers of Android related content can view the site: "Summary of the use of Android controls," "The Summary of Android view tips," "Android File Operation tips Summary", "Android operation SQLite Database Skills Summary", " Android operations JSON format Data tips summary, "Android Database Operation skills Summary", "Android programming activity Operation Skills Summary", "Android programming development of SD card operation method Summary", "Android Development and advanced tutorial And the Android resource Operation Skills Summary
I hope this article will help you with the Android program.