LauncherActivity
可以启动别的程序的窗口,当然了,自己直接写个普通的activity也可以实现
item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:paddingTop="20dp" android:paddingBottom="20dp" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:paddingRight="20dp" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/imageView1" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:text="Small Text" android:textAppearance="?android:attr/textAppearanceSmall" /> </RelativeLayout>
MainActivity.java
继承LauncherActivity,需要设置ListAdapter,并实现Intent intentForPosition(int position)
public class MainActivity extends LauncherActivity { List<String> classes = new ArrayList<String>(); protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 获取应用程序 List<PackageInfo> pags = getPackageManager().getInstalledPackages(0); List<Map<String, Object>> data = new ArrayList<Map<String, Object>>(); for (int i = 0; i < pags.size(); i++) { PackageInfo pag = pags.get(i); // 用户程序 //if ((pag.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) { Map<String, Object> map = new HashMap<String, Object>(); // 程序名字 map.put("name",pag.applicationInfo.loadLabel(getPackageManager()).toString()); // 图标 map.put("drawable",pag.applicationInfo.loadIcon(getPackageManager())); map.put("packageName", pag.packageName); classes.add(pag.packageName); data.add(map); //} } SimpleAdapter sa = new SimpleAdapter(MainActivity.this, data, R.layout.item, new String[] { "name", "drawable", "packageName" }, new int[] { R.id.textView1, R.id.imageView1, R.id.textView2 }); setListAdapter(sa); // 将drawablei显示到imageview里 sa.setViewBinder(new ViewBinder() { public boolean setViewValue(View view, Object data, String textRepresentation) { if (view instanceof ImageView && data instanceof Drawable) { ImageView iv = (ImageView) view; iv.setImageDrawable((Drawable) data); return true; } else return false; } }); } protected Intent intentForPosition(int position) { Intent intent = this.getPackageManager().getLaunchIntentForPackage( classes.get(position)); return intent; } }
由于LancherActivity已经内置了list,因此直接实现getTargetIntent就可以了
MainActivity
public class MainActivity extends LauncherActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } protected Intent getTargetIntent() { // TODO Auto-generated method stub Intent intent=new Intent(Intent.ACTION_MAIN); intent.addCategory("android.intent.category.LAUNCHER"); return intent; } }