你們好,我今天給你們簡單講一下Launcher裏如何列出全部安裝的應用的,咱們點擊Launcher的抽屜(SlidingDrawer)就會 出現全部安裝的應用列表(固然Widget,Launcher自己除外).而且點擊應用圖標進入 相關應用。我這裏就先簡單的用一個GridView來顯示應用。java
老樣子我還會寫一個簡單的Demo讓你們理解。你們跟着個人步驟來。android
第一步:新建一個Android工程命名爲:Launcher.app
第二步:修改main.xml佈局文件,代碼以下(只有一個GridView這裏):ide
- <?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/allapps"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
第三步:新建一個application_layout.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"
- >
- <ImageView
- android:id="@+id/app_icon"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- <TextView
- android:id="@+id/app_title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
第四步:也就是核心了,修改Launcher.java代碼以下:佈局
- package com.tutor.launcher;
- import java.util.Collections;
- import java.util.List;
- import android.app.Activity;
- import android.content.ComponentName;
- import android.content.Context;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.content.pm.ResolveInfo;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AdapterView;
- import android.widget.BaseAdapter;
- import android.widget.GridView;
- import android.widget.ImageView;
- import android.widget.TextView;
- import android.widget.AdapterView.OnItemClickListener;
- public class Launcher extends Activity implements OnItemClickListener{
-
- private GridView mGridView;
- private Context mContext;
- private PackageManager mPackageManager;
- private List<ResolveInfo> mAllApps;
-
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- setupViews();
-
- }
-
- public void setupViews(){
- mContext = Launcher.this;
- mPackageManager = getPackageManager();
- mGridView = (GridView)findViewById(R.id.allapps);
- bindAllApps();
-
- mGridView.setAdapter(new GridItemAdapter(mContext, mAllApps));
- mGridView.setNumColumns(4);
- mGridView.setOnItemClickListener(this);
- }
-
- public void bindAllApps(){
-
-
- Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
- mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
-
- mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);
- Collections.sort(mAllApps, new ResolveInfo.DisplayNameComparator(mPackageManager));
- }
-
-
-
-
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
-
- ResolveInfo res = mAllApps.get(position);
-
- String pkg = res.activityInfo.packageName;
- String cls = res.activityInfo.name;
-
- ComponentName componet = new ComponentName(pkg, cls);
-
- Intent i = new Intent();
- i.setComponent(componet);
- startActivity(i);
- }
-
-
- private class GridItemAdapter extends BaseAdapter{
- private Context context;
- private List<ResolveInfo> resInfo;
-
-
- public GridItemAdapter(Context c,List<ResolveInfo> res){
- context = c;
- resInfo = res;
- }
- @Override
- public int getCount() {
-
- return resInfo.size();
- }
- @Override
- public Object getItem(int position) {
-
- return null;
- }
- @Override
- public long getItemId(int position) {
-
- return 0;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
-
-
- convertView = LayoutInflater.from(context)
- .inflate(R.layout.application_layout, null);
-
- ImageView app_icon = (ImageView)convertView.findViewById(R.id.app_icon);
- TextView app_tilte = (TextView)convertView.findViewById(R.id.app_title);
-
- ResolveInfo res = resInfo.get(position);
- app_icon.setImageDrawable(res.loadIcon(mPackageManager));
- app_tilte.setText(res.loadLabel(mPackageManager).toString());
- return convertView;
- }
-
- }
- }
第五步:運行以上工程,獲得的效果圖以下:this
圖1.列出全部安裝的應用.spa
圖2.點擊aQQ應用進入到相應的應用裏。.net