36, get the application icon and the corresponding software name, using the GridView way to show

Source: Internet
Author: User

------------------------------Main.java--------------------------


Package Com.example.rfe;


Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;


Import Android.app.ProgressDialog;
Import Android.content.pm.ApplicationInfo;
Import Android.content.pm.PackageManager;
Import android.graphics.drawable.Drawable;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import android.support.v7.app.ActionBarActivity;
Import Android.util.Log;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.widget.BaseAdapter;
Import Android.widget.GridView;
Import Android.widget.ImageView;
Import Android.widget.TextView;


/**
* Get the installed software name and the package name of the software, which can be used to open another application
*
* @author Administrator
*
*/
public class Mainactivity extends Actionbaractivity implements Runnable {


list<map<string, object>> list = null;
Private ProgressDialog PD;
Private Packagemanager Packagemanager;
GridView List_home;
@Override
public void OnCreate (Bundle icicle) {
Super.oncreate (Icicle);
Setcontentview (R.layout.activity_main);
Settitle ("Software Information");
List_home = (GridView) Findviewbyid (r.id.list_home);
PD = Progressdialog.show (This, "Please wait ...", "Collecting information on software you have installed ...", true,
FALSE);
Thread thread = new thread (this);
Thread.Start ();
}


private void Refreshlistitems () {
List = Fetch_installed_apps ();
Myadapter myadapter = new Myadapter ();
List_home.setadapter (Myadapter);
Set the title of Actionbar!!
Settitle ("Software information, already installed" + list.size () + "application.");
}


Public List Fetch_installed_apps () {
list<applicationinfo> packages = Getpackagemanager ()
. getinstalledapplications (0);
List = new arraylist<map<string, object>> (Packages.size ());
iterator<applicationinfo> L = packages.iterator ();
Packagemanager = Getpackagemanager ();
while (L.hasnext ()) {
map<string, object> map = new hashmap<string, object> ();
ApplicationInfo app = (applicationinfo) l.next ();
drawable drawable = App.loadicon (Packagemanager);
String label = "";
try {
label = Getpackagemanager (). Getapplicationlabel (APP). toString ();
} catch (Exception e) {
LOG.I ("Exception", e.tostring ());
}
Map = new hashmap<string, object> ();
Map.put ("name", label);
Map.put ("ico", drawable);
List.add (map);
}
return list;
}


@Override
public void Run () {
Fetch_installed_apps ();
Handler.sendemptymessage (0);
}


Private Handler Handler = new Handler () {
public void Handlemessage (Message msg) {
Refreshlistitems ();
Pd.dismiss ();
}
};



Private class Myadapter extends baseadapter{


@Override
public int GetCount () {
return List.size ();
}

@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
View view = View.inflate (mainactivity.this, r.layout.list_item, NULL);
ImageView Iv_item = (ImageView) View.findviewbyid (R.id.iv_item);
TextView Tv_item = (TextView) View.findviewbyid (R.id.tv_item);

Tv_item.settext ((charsequence) list.get (position). Get ("name"));
Drawable drawable = (drawable) list.get (position). Get ("ico");
Iv_item.setimagedrawable (drawable);
return view;
}


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


@Override
public long getitemid (int position) {
return 0;
}

}
}


。。。。。。。。。。。。 Main.xml ......


<?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" >




<gridview
Android:id= "@+id/list_home"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:layout_margintop= "15dip"
Android:numcolumns= "3"
android:verticalspacing= "10dip"/>
</LinearLayout>


。。。。。。。。。。。。。 List_item.xml .........


<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:gravity= "Center"
android:orientation= "Vertical" >


<imageview
Android:id= "@+id/iv_item"
Android:layout_width= "60dip"
android:layout_height= "60dip"
android:src= "@drawable/ic_launcher"/>


<textview
Android:id= "@+id/tv_item"
Android:textcolor= "#000000"
Android:textsize= "16SP"
android:text= "mobile phone anti-theft"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>


</LinearLayout>



36, get the application icon and the corresponding software name, using the GridView way to show

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.