27090213_27us   

当点击《愤怒的小鸟》时,将打开《愤怒的小鸟》这个游戏

27090213_bCMW  main.xml的源代码如下:

01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
03     android:orientation="vertical"
04     android:layout_width="fill_parent"
05     android:layout_height="fill_parent"
06     >
07     <GridView xmlns:android="http://schemas.android.com/apk/res/android"
08         android:id="@+id/gridview"
09         android:layout_width="fill_parent"
10         android:layout_height="fill_parent"
11         android:numColumns="auto_fit"
12         android:verticalSpacing="15dp"
13         android:horizontalSpacing="12dp"
14         android:columnWidth="95dp"
15         android:stretchMode="columnWidth"
16         android:gravity="center"
17         />
18 </LinearLayout>

 

接着是apps.xml,源代码如下:

01 <?xml version="1.0" encoding="utf-8"?>
02 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
03     android:orientation="vertical"      
04     android:layout_width="fill_parent"    
05     android:layout_height="fill_parent"     
06     android:gravity="center" >
07   
08     <ImageView
09         android:id="@+id/apps_image"        
10         android:layout_width="48dip"     
11         android:layout_height="48dip"       
12         android:icon="@drawable/icon" />    
13   
14     <TextView        
15         android:id="@+id/apps_textview"     
16         android:layout_width="wrap_content"     
17         android:layout_height="wrap_content"        
18         android:maxLines="1"        
19         android:maxEms="5"      
20         android:text="good" />
21   
22 </LinearLayout>

接下来就是java源代码了,比较长,希望耐心看下去

001 package com.android.game;
002   
003 import java.util.ArrayList;
004 import java.util.List;
005   
006 import android.app.Activity;
007 import android.content.Context;
008 import android.content.Intent;
009 import android.content.pm.PackageInfo;
010 import android.content.pm.PackageManager;
011 import android.graphics.drawable.Drawable;
012 import android.os.Bundle;
013 import android.view.LayoutInflater;
014 import android.view.View;
015 import android.view.ViewGroup;
016 import android.widget.AdapterView;
017 import android.widget.BaseAdapter;
018 import android.widget.GridView;
019 import android.widget.ImageView;
020 import android.widget.TextView;
021 import android.widget.AdapterView.OnItemClickListener;
022   
023 public class Apps extends Activity {
024   
025     // 用来记录应用程序的信息
026     List<AppsItemInfo> list;
027   
028     private GridView gridview;
029     private PackageManager pManager;
030   
031     @Override
032     protected void onCreate(Bundle savedInstanceState) {
033         // TODO Auto-generated method stub
034         super.onCreate(savedInstanceState);
035         this.setContentView(R.layout.main);
036   
037         // 取得gridview
038         gridview = (GridView) findViewById(R.id.gridview);
039   
040         // 获取图片、应用名、包名
041         pManager = Apps.this.getPackageManager();
042         List<PackageInfo> appList = getAllApps(Apps.this);
043   
044         list = new ArrayList<AppsItemInfo>();
045   
046         for (int i = 0; i < appList.size(); i++) {
047             PackageInfo pinfo = appList.get(i);
048             AppsItemInfo shareItem = new AppsItemInfo();
049             // 设置图片
050             shareItem.setIcon(pManager
051                     .getApplicationIcon(pinfo.applicationInfo));
052             // 设置应用程序名字
053             shareItem.setLabel(pManager.getApplicationLabel(
054                     pinfo.applicationInfo).toString());
055             // 设置应用程序的包名
056             shareItem.setPackageName(pinfo.applicationInfo.packageName);
057   
058             list.add(shareItem);
059   
060         }
061   
062         // 设置gridview的Adapter
063         gridview.setAdapter(new baseAdapter());
064   
065         // 点击应用图标时,做出响应
066         gridview.setOnItemClickListener(new ClickListener());
067   
068     }
069   
070     public static List<PackageInfo> getAllApps(Context context) {
071   
072         List<PackageInfo> apps = new ArrayList<PackageInfo>();
073         PackageManager pManager = context.getPackageManager();
074         // 获取手机内所有应用
075         List<PackageInfo> packlist = pManager.getInstalledPackages(0);
076         for (int i = 0; i < packlist.size(); i++) {
077             PackageInfo pak = (PackageInfo) packlist.get(i);
078   
079             // 判断是否为非系统预装的应用程序
080             // 这里还可以添加系统自带的,这里就先不添加了,如果有需要可以自己添加
081             // if()里的值如果<=0则为自己装的程序,否则为系统工程自带
082             if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
083                 // 添加自己已经安装的应用程序
084                 apps.add(pak);
085             }
086   
087         }
088         return apps;
089     }
090   
091     private class baseAdapter extends BaseAdapter {
092         LayoutInflater inflater = LayoutInflater.from(Apps.this);
093   
094         @Override
095         public int getCount() {
096             // TODO Auto-generated method stub
097             return list.size();
098         }
099   
100         @Override
101         public Object getItem(int position) {
102             // TODO Auto-generated method stub
103             return null;
104         }
105   
106         @Override
107         public long getItemId(int position) {
108             // TODO Auto-generated method stub
109             return position;
110         }
111   
112         @Override
113         public View getView(int position, View convertView, ViewGroup parent) {
114             // TODO Auto-generated method stub
115             ViewHolder holder;
116             if (convertView == null) {
117                 // 使用View的对象itemView与R.layout.item关联
118                 convertView = inflater.inflate(R.layout.apps, null);
119                 holder = new ViewHolder();
120                 holder.icon = (ImageView) convertView
121                         .findViewById(R.id.apps_image);
122                 holder.label = (TextView) convertView
123                         .findViewById(R.id.apps_textview);
124                 convertView.setTag(holder);
125             } else {
126                 holder = (ViewHolder) convertView.getTag();
127             }
128   
129             holder.icon.setImageDrawable(list.get(position).getIcon());
130             holder.label.setText(list.get(position).getLabel().toString());
131   
132             return convertView;
133   
134         }
135   
136     }
137   
138     // 当用户点击应用程序图标时,将对这个类做出响应
139     private class ClickListener implements OnItemClickListener {
140   
141         @Override
142         public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
143                 long arg3) {
144   
145             // 将应用所选的应用程序信息共享到Application中
146             MyApp appState = ((MyApp) getApplicationContext());
147             // 获取当前所在选项卡
148             String tab_id = appState.getTab_id();
149             // 设置所选应用程序信息
150             appState.set_AppInfo(tab_id, list.get(arg2).getLabel(), list.get(
151                     arg2).getIcon(), list.get(arg2).getPackageName());      
152             Intent intent = new Intent(Apps.this, Game.class);
153             startActivity(intent);
154             // 销毁当前Activity
155             finish();
156         }
157   
158     }
159   
160     // 自定义一个 AppsItemInfo 类,用来存储应用程序的相关信息
161     private class AppsItemInfo {
162   
163         private Drawable icon; // 存放图片
164         private String label; // 存放应用程序名
165         private String packageName; // 存放应用程序包名
166   
167         public Drawable getIcon() {
168             return icon;
169         }
170   
171         public void setIcon(Drawable icon) {
172             this.icon = icon;
173         }
174   
175         public String getLabel() {
176             return label;
177         }
178   
179         public void setLabel(String label) {
180             this.label = label;
181         }
182   
183         public String getPackageName() {
184             return packageName;
185         }
186   
187         public void setPackageName(String packageName) {
188             this.packageName = packageName;
189         }
190   
191     }
192   
193 }

在此就写完了,写得不好的地方,请高手不吝赐教

文章出处:http://blog.csdn.net/hugengzong/article/details/6366806

創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()