Android obtains all installed applications and displays them on listview.

Source: Internet
Author: User

First view;

Read the code in detail;

Package COM. taskmanage. file; import Java. util. arraylist; import Java. util. collections; import Java. util. hashmap; import Java. util. iterator; import Java. util. list; import Java. util. map; import android. app. activity; import android. app. progressdialog; import android. content. componentname; import android. content. context; import android. content. intent; import android. content. PM. applicationinfo; import android. conten T. PM. packagemanager; import android. content. PM. resolveinfo; import android. graphics. drawable. drawable; import android.net. uri; import android. OS. build; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. provider. settings; import android. util. log; import android. view. layoutinflater; import android. view. view; import android. view. viewgroup; import android. widget. abslist View; import android. widget. adapterview; import android. widget. abslistview. onscrolllistener; import android. widget. adapterview. onitemclicklistener; import android. widget. baseadapter; import android. widget. imageview; import android. widget. listview; import android. widget. simpleadapter; import android. widget. textview; public class softactivity extends activity implements runnable, onitemclicklistener {private Static final string scheme = "package"; Private Static final string app_pkg_name_21 = "com. android. settings. applicationpkgname "; Private Static final string app_pkg_name_22 =" PKG "; Private Static final string app_details_package_name =" com. android. settings "; Private Static final string app_details_class_name =" com. android. settings. installedappdetails "; private list <Map <string, Object> List = NULL; Private listview softlist = NULL; private progressdialog PD; private context mcontext; private packagemanager release; private list <resolveinfo> mallapps; @ overrideprotected void oncreate (bundle savedinstancestate) {setcontentview (R. layout. software); settitle ("File Manager"); mcontext = This; mpackagemanager = getpackagemanager (); softlist = (listview) findviewbyid (R. id. softlist); Pd = progressdialog. sh Ow (this, "Please wait .. "," collecting software information... ", true, false); thread = new thread (this); thread. start (); super. oncreate (savedinstancestate);}/*** check the system application and add it to the Application List */private void bindmsg () {// apply the filter condition intent mainintent = new intent (intent. action_main, null); mainintent. addcategory (intent. category_launcher); mallapps = mpackagemanager. queryintentactivities (mainintent, 0); softlist. setadapter (New myadapter (mcon Text, mallapps); softlist. setonitemclicklistener (this); // sort collections by registration. sort (mallapps, new resolveinfo. displaynamecomparator (mpackagemanager) ;}@ overridepublic void run () {bindmsg (); handler. sendemptymessage (0);} private handler = new handler () {public void handlemessage (Message MSG) {PD. dismiss () ;}}; class myadapter extends baseadapter {private context; private list <resolv Einfo> resinfo; private imageview app_icon = NULL; private textview app_tilte = NULL, app_des = NULL; private resolveinfo res; private layoutinflater infater = NULL; Public myadapter (context, list <resolveinfo> resinfo) {This. context = context; this. resinfo = resinfo; infater = (layoutinflater) context. getsystemservice (context. layout_inflater_service);} @ overridepublic int getcount () {return resinfo. s Ize () ;}@ overridepublic object getitem (INT arg0) {return arg0 ;}@ overridepublic long getitemid (INT position) {return position ;}@ overridepublic view getview (INT position, view convertview, viewgroup parent) {// view = NULL; viewholder holder = NULL; If (convertview = NULL | convertview. gettag () = NULL) {convertview = infater. inflate (R. layout. soft_row, null); holder = new viewholder (convertvie W); convertview. settag (holder);} else {// view = convertview; holder = (viewholder) convertview. gettag () ;}// get the application package name, program name, and program icon res = resinfo. get (position); holder. appicon. setimagedrawable (res. loadicon (mpackagemanager); holder. tvapplabel. settext (res. loadlabel (mpackagemanager ). tostring (); holder. tvpkgname. settext (res. activityinfo. packagename + '\ n' + Res. activityinfo. name); Return convertview;/* Co Nvertview = layoutinflater. from (context ). inflate (R. layout. soft_row, null); app_icon = (imageview) convertview. findviewbyid (R. id. IMG); app_tilte = (textview) convertview. findviewbyid (R. id. name); app_des = (textview) convertview. findviewbyid (R. id. DESC); Res = resinfo. get (position); app_icon.setimagedrawable (res. loadicon (mpackagemanager); app_tilte.settext (res. loadlabel (mpackagemanager ). tostring (); app_des.s Ettext (res. activityinfo. packagename + '\ n' + Res. activityinfo. name); Return convertview; * // set the class viewholder {imageview appicon; textview tvapplabel; textview tvpkgname; Public viewholder (view) {This. appicon = (imageview) view. findviewbyid (R. id. IMG); this. tvapplabel = (textview) view. findviewbyid (R. id. name); this. tvpkgname = (textview) view. findviewbyid (R. id. DESC);}/*** click the application to enter System Application Management Interface */@ overridepublic void onitemclick (adapterview <?> Arg0, view arg1, int position, long arg3) {resolveinfo res = mallapps. get (position); string PKG = res. activityinfo. packagename; intent = new intent (); Final int apilevel = build. version. sdk_int; If (apilevel> = 9) {// intent later than Version 2.2. setaction (settings. action_application_details_settings); Uri uri = Uri. fromparts (scheme, PKG, null); intent. setdata (URI);} else {// final string apppkgname = (apilev El = 8? App_pkg_name_22: app_pkg_name_21); intent. setaction (intent. action_view); intent. setclassname (intent, intent); intent. putextra (apppkgname, PKG);} startactivity (intent );}}

Software. xml layout;

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><ListView     android:layout_width="fill_parent"android:layout_height="fill_parent" android:drawSelectorOnTop="false"android:id="@+id/softlist" /></LinearLayout>

Soft_row.xml layout;

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/vw1"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:orientation="horizontal">        <ImageView android:id="@+id/img"        android:layout_width="32dip"        android:layout_margin="4dip"        android:layout_height="32dip"/>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="vertical">        <TextView android:id="@+id/name"            android:textSize="18sp"            android:textStyle="bold"            android:layout_width="fill_parent"            android:layout_height="wrap_content"/>        <TextView android:id="@+id/desc"            android:textSize="14sp"            android:layout_width="fill_parent"            android:paddingLeft="10dip"            android:layout_height="wrap_content"/>    </LinearLayout></LinearLayout>

Do not forget to add permissions;

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.GET_TASKS"/>

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.