[size=1em][size=1em]package com.android.game;
[size=1em]import java.util.ArrayList;
[size=1em]import java.util.List;
[size=1em]import android.app.Activity;
[size=1em]import android.content.Context;
[size=1em]import android.content.Intent;
[size=1em]import android.content.pm.PackageInfo;
[size=1em]import android.content.pm.PackageManager;
[size=1em]import android.graphics.drawable.Drawable;
[size=1em]import android.os.Bundle;
[size=1em]import android.view.LayoutInflater;
[size=1em]import android.view.View;
[size=1em]import android.view.ViewGroup;
[size=1em]import android.widget.AdapterView;
[size=1em]import android.widget.BaseAdapter;
[size=1em]import android.widget.GridView;
[size=1em]import android.widget.ImageView;
[size=1em]import android.widget.TextView;
[size=1em]import android.widget.AdapterView.OnItemClickListener;
[size=1em]public class Apps extends Activity {
[size=1em] // 用来记录应用程序的信息
[size=1em] List<AppsItemInfo> list;
[size=1em] private GridView gridview;
[size=1em] private PackageManager pManager;
[size=1em] @Override
[size=1em] protected void onCreate(Bundle savedInstanceState) {
[size=1em] // TODO Auto-generated method stub
[size=1em] super.onCreate(savedInstanceState);
[size=1em] this.setContentView(R.layout.main);
[size=1em] // 取得gridview
[size=1em] gridview = (GridView) findViewById(R.id.gridview);
[size=1em] // 获取图片、应用名、包名
[size=1em] pManager = Apps.this.getPackageManager();
[size=1em] List<
ackageInfo> appList = getAllApps(Apps.this);
[size=1em] list = new ArrayList<AppsItemInfo>();
[size=1em] for (int i = 0; i < appList.size(); i++) {
[size=1em] PackageInfo pinfo = appList.get(i);
[size=1em] AppsItemInfo shareItem = new AppsItemInfo();
[size=1em] // 设置图片
[size=1em] shareItem.setIcon(pManager
[size=1em] .getApplicationIcon(pinfo.applicationInfo));
[size=1em] // 设置应用程序名字
[size=1em] shareItem.setLabel(pManager.getApplicationLabel(
[size=1em] pinfo.applicationInfo).toString());
[size=1em] // 设置应用程序的包名
[size=1em] shareItem.setPackageName(pinfo.applicationInfo.packageName);
[size=1em] list.add(shareItem);
[size=1em] }
[size=1em] // 设置gridview的Adapter
[size=1em] gridview.setAdapter(new baseAdapter());
[size=1em] // 点击应用图标时,做出响应
[size=1em] gridview.setOnItemClickListener(new ClickListener());
[size=1em] }
[size=1em] public static List<
ackageInfo> getAllApps(Context context) {
[size=1em] List<
ackageInfo> apps = new ArrayList<
ackageInfo>();
[size=1em] PackageManager pManager = context.getPackageManager();
[size=1em] // 获取手机内所有应用
[size=1em] List<
ackageInfo> packlist = pManager.getInstalledPackages(0);
[size=1em] for (int i = 0; i < packlist.size(); i++) {
[size=1em] PackageInfo pak = (PackageInfo) packlist.get(i);
[size=1em] // 判断是否为非系统预装的应用程序
[size=1em] // 这里还可以添加系统自带的,这里就先不添加了,如果有需要可以自己添加
[size=1em] // if()里的值如果<=0则为自己装的程序,否则为系统工程自带
[size=1em] if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
[size=1em] // 添加自己已经安装的应用程序
[size=1em] apps.add(pak);
[size=1em] }
[size=1em] }
[size=1em] return apps;
[size=1em] }
[size=1em] private class baseAdapter extends BaseAdapter {
[size=1em] LayoutInflater inflater = LayoutInflater.from(Apps.this);
[size=1em] @Override
[size=1em] public int getCount() {
[size=1em] // TODO Auto-generated method stub
[size=1em] return list.size();
[size=1em] }
[size=1em] @Override
[size=1em] public Object getItem(int position) {
[size=1em] // TODO Auto-generated method stub
[size=1em] return null;
[size=1em] }
[size=1em] @Override
[size=1em] public long getItemId(int position) {
[size=1em] // TODO Auto-generated method stub
[size=1em] return position;
[size=1em] }
[size=1em] @Override
[size=1em] public View getView(int position, View convertView, ViewGroup parent) {
[size=1em] // TODO Auto-generated method stub
[size=1em] ViewHolder holder;
[size=1em] if (convertView == null) {
[size=1em] // 使用View的对象itemView与R.layout.item关联
[size=1em] convertView = inflater.inflate(R.layout.apps, null);
[size=1em] holder = new ViewHolder();
[size=1em] holder.icon = (ImageView) convertView
[size=1em] .findViewById(R.id.apps_image);
[size=1em] holder.label = (TextView) convertView
[size=1em] .findViewById(R.id.apps_textview);
[size=1em] convertView.setTag(holder);
[size=1em] } else {
[size=1em] holder = (ViewHolder) convertView.getTag();
[size=1em] }
[size=1em] holder.icon.setImageDrawable(list.get(position).getIcon());
[size=1em] holder.label.setText(list.get(position).getLabel().toString());
[size=1em] return convertView;
[size=1em] }
[size=1em] }
[size=1em] // 当用户点击应用程序图标时,将对这个类做出响应
[size=1em] private class ClickListener implements OnItemClickListener {
[size=1em] @Override
[size=1em] public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
[size=1em] long arg3) {
[size=1em] // 将应用所选的应用程序信息共享到Application中
[size=1em] MyApp appState = ((MyApp) getApplicationContext());
[size=1em] // 获取当前所在选项卡
[size=1em] String tab_id = appState.getTab_id();
[size=1em] // 设置所选应用程序信息
[size=1em] appState.set_AppInfo(tab_id, list.get(arg2).getLabel(), list.get(
[size=1em] arg2).getIcon(), list.get(arg2).getPackageName());
[size=1em] Intent intent = new Intent(Apps.this, Game.class);
[size=1em] startActivity(intent);
[size=1em] // 销毁当前Activity
[size=1em] finish();
[size=1em] }
[size=1em] }
[size=1em] // 自定义一个 AppsItemInfo 类,用来存储应用程序的相关信息
[size=1em] private class AppsItemInfo {
[size=1em] private Drawable icon; // 存放图片
[size=1em] private String label; // 存放应用程序名
[size=1em] private String packageName; // 存放应用程序包名
[size=1em] public Drawable getIcon() {
[size=1em] return icon;
[size=1em] }
[size=1em] public void setIcon(Drawable icon) {
[size=1em] this.icon = icon;
[size=1em] }
[size=1em] public String getLabel() {
[size=1em] return label;
[size=1em] }
[size=1em] public void setLabel(String label) {
[size=1em] this.label = label;
[size=1em] }
[size=1em] public String getPackageName() {
[size=1em] return packageName;
[size=1em] }
[size=1em] public void setPackageName(String packageName) {
[size=1em] this.packageName = packageName;
[size=1em] }
[size=1em] }
[size=1em]}