Android中Launcher实例

Launcher是Android的应用程序启动器,Launcher的功能 还包含:桌面的切换、应用程序快捷 (shortcut)功能、背景图(Wallpaper)功能等等。在Android的桌面最下方,有一个图示,按下后可以拉出应用程序图示清单,这是Launcher提供的功能。

我们这个实例是用GridView来实现Launcher效果的, 下面的截屏显示本实例的效果图:


源代码如下:

main.xml布局文件,只有一个GridView:

 

GridView的每一个Item也需要一个布局文件,用来定义应用的图标与标题,application_layout.xml:

 

主程序文件,Launcher.java:

package com.test.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 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.gridview); bindAllApps(); // GridView中放入数据 mGridView.setAdapter(new GridItemAdapter(mContext, mAllApps)); mGridView.setNumColumns(4); mGridView.setOnItemClickListener(this); } public void bindAllApps() { // 应用中总有一个activity申明成这两个属性,也就是应用的入口 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); // 该应用的包名和主Activity String pkg = res.activityInfo.packageName; String cls = res.activityInfo.name; ComponentName componet = new ComponentName(pkg, cls); Intent intent = new Intent(); intent.setComponent(componet); startActivity(intent); } private class GridItemAdapter extends BaseAdapter { private Context context; private List resInfo; public GridItemAdapter(Context c, List 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 imageView = (ImageView) convertView .findViewById(R.id.imageview); TextView textView = (TextView) convertView .findViewById(R.id.textview); ResolveInfo res = resInfo.get(position); imageView.setImageDrawable(res.loadIcon(mPackageManager)); textView.setText(res.loadLabel(mPackageManager).toString()); return convertView; } } }

 

几点说明:

  1. 函数setupViews()是建立Launcher桌面的Views的。setupViews里面的函数bindAllApps()是把Activity里面所有符合特定Intent的结果查出来,结果封装在ResolveInfo的List里面。可以通过ResolveInfo获取到应用的图标及标签。
  2. 包android.content.pm所含的类负责获取应用程序包的信息,包括关于activities,permissions,services,signatures和providers的信息。

 

你可能感兴趣的:(Android中Launcher实例)