launcher初步模型

这个比较简陋,只是自己随便练手瞎写的,以后会继续完善

1.将自己的程序加入到系统launcher选项中

在manifest文件中自己的主程序中的intentFilter中加入如下两行代码即可

<category android:name="android.intent.category.HOME" />

 <category android:name="android.intent.category.DEFAULT" />


2.用GridView显示所有安装的程序,并添加点击事件


main.xml

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

    <GridView android:layout_width="fill_parent"
        android:id="@+id/apps"
        android:numColumns="4"
        android:layout_height="wrap_content">
    </GridView>

</LinearLayout>


java程序

package com.test.lancher;

import java.util.List;

import android.app.Activity;
import android.appwidget.AppWidgetHost;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProviderInfo;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class LancherActivity extends Activity {
	GridView mGrid;
	private List<ResolveInfo> mApps;

	private OnItemClickListener listener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			//程序信息
			ResolveInfo info = mApps.get(arg2);

			//包名
			String pkg = info.activityInfo.packageName;
			// 主activity类名
			String cls = info.activityInfo.name;

			ComponentName componet = new ComponentName(pkg, cls);

			Intent i = new Intent();
			i.setComponent(componet);
			startActivity(i);
		}
	
	};
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		loadApps();
		setContentView(R.layout.main);
		mGrid = (GridView) findViewById(R.id.apps);
		mGrid.setAdapter(new AppsAdapter());
		mGrid.setOnItemClickListener(listener);
	}
//获取所有程序的信息
	private void loadApps() {
		Intent mIntent = new Intent(Intent.ACTION_MAIN, null);
		mIntent.addCategory(Intent.CATEGORY_LAUNCHER);

		mApps = getPackageManager().queryIntentActivities(mIntent, 0);
	}

	public class AppsAdapter extends BaseAdapter {
		public AppsAdapter() {
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView i;

			if (convertView == null) {
				i = new ImageView(LancherActivity.this);
				i.setScaleType(ImageView.ScaleType.FIT_CENTER);
				i.setLayoutParams(new GridView.LayoutParams(50, 50));
			} else {
				i = (ImageView) convertView;
			}

			ResolveInfo info = mApps.get(position);
			i.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));

			return i;
		}

		public final int getCount() {
			return mApps.size();
		}

		public final Object getItem(int position) {
			return mApps.get(position);
		}

		public final long getItemId(int position) {
			return position;
		}
	}
}




你可能感兴趣的:(android,String,layout,null,Class,encoding)