<?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"><--主界面布局--> <GridViewandroid:gravity="center_horizontal"android:stretchMode="columnWidth"android:columnWidth="100dp"android:horizontalSpacing="10dp"android:verticalSpacing="10dp"android:layout_width="match_parent"android:id="@+id/apps_list"android:numColumns="3"android:layout_height="match_parent"> </GridView> </LinearLayout>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
<--清单文件中的配置--><application android:name="com.way.launcher.SceneLauncherApplication" android:icon="@drawable/ic_launcher_home" android:label="@string/application_name" android:theme="@android:style/Theme.Black.NoTitleBar" > <activity android:name="com.way.launcher.SceneLauncherActivity" android:configChanges="orientation|keyboardHidden" android:launchMode="singleTask" android:screenOrientation="nosensor" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.HOME" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.way.launcher.SceneChooserActivity" android:configChanges="orientation|keyboardHidden" android:launchMode="singleTask" android:screenOrientation="nosensor" /></application>
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
public class MyHome extends Activity {private List<ResolveInfo> mApps;GridView mGrid;private AdapterView.OnItemClickListener listener = new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,long id) {ResolveInfo info = mApps.get(position);//设置点击item监听String pkg = info.activityInfo.packageName;//该应用的包名String cls = info.activityInfo.name; //应用的主activity类ComponentName componet = new ComponentName(pkg, cls);//设置跳转Intent i = new Intent();//发送意图i.setComponent(componet);startActivity(i);//启动被点击的activity} }; @Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);loadApps();setContentView(R.layout.activity_main);mGrid = (GridView) findViewById(R.id.apps_list);mGrid.setAdapter(new AppsAdapter());//由于adapter是内部类,所以直接将数据传入adapter里面了mGrid.setOnItemClickListener(listener);}private void loadApps() {//查询所有安装的应用Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);mApps = getPackageManager().queryIntentActivities(mainIntent, 0);} public class AppsAdapter extends BaseAdapter {public AppsAdapter() {}public View getView(int position, View convertView, ViewGroup parent) {ImageView imageView;if (convertView == null) { imageView = new ImageView(MyHome.this);imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);imageView.setLayoutParams(new GridView.LayoutParams(100, 100));} else { imageView = (ImageView) convertView;}ResolveInfo info = mApps.get(position);imageView.setImageDrawable(info.activityInfo.loadIcon(getPackageManager()));return imageView;}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;}} @Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK) {return false;}//屏蔽返回键return super.onKeyDown(keyCode, event);}}
时间: 2024-10-11 10:20:01