当点击《愤怒的小鸟》时,将打开《愤怒的小鸟》这个游戏
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" |
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" |
接着是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" > |
09 |
android:id = "@+id/apps_image" |
10 |
android:layout_width = "48dip" |
11 |
android:layout_height = "48dip" |
12 |
android:icon = "@drawable/icon" /> |
15 |
android:id = "@+id/apps_textview" |
16 |
android:layout_width = "wrap_content" |
17 |
android:layout_height = "wrap_content" |
20 |
android:text = "good" /> |
接下来就是java源代码了,比较长,希望耐心看下去
001 |
package com.android.game; |
003 |
import java.util.ArrayList; |
004 |
import java.util.List; |
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; |
023 |
public class Apps extends Activity { |
026 |
List<AppsItemInfo> list; |
028 |
private GridView gridview; |
029 |
private PackageManager pManager; |
032 |
protected void onCreate(Bundle savedInstanceState) { |
033 |
// TODO Auto-generated method stub |
034 |
super .onCreate(savedInstanceState); |
035 |
this .setContentView(R.layout.main); |
038 |
gridview = (GridView) findViewById(R.id.gridview); |
041 |
pManager = Apps. this .getPackageManager(); |
042 |
List<PackageInfo> appList = getAllApps(Apps. this ); |
044 |
list = new ArrayList<AppsItemInfo>(); |
046 |
for ( int i = 0 ; i < appList.size(); i++) { |
047 |
PackageInfo pinfo = appList.get(i); |
048 |
AppsItemInfo shareItem = new AppsItemInfo(); |
050 |
shareItem.setIcon(pManager |
051 |
.getApplicationIcon(pinfo.applicationInfo)); |
053 |
shareItem.setLabel(pManager.getApplicationLabel( |
054 |
pinfo.applicationInfo).toString()); |
056 |
shareItem.setPackageName(pinfo.applicationInfo.packageName); |
062 |
// 设置gridview的Adapter |
063 |
gridview.setAdapter( new baseAdapter()); |
066 |
gridview.setOnItemClickListener( new ClickListener()); |
070 |
public static List<PackageInfo> getAllApps(Context context) { |
072 |
List<PackageInfo> apps = new ArrayList<PackageInfo>(); |
073 |
PackageManager pManager = context.getPackageManager(); |
075 |
List<PackageInfo> packlist = pManager.getInstalledPackages( 0 ); |
076 |
for ( int i = 0 ; i < packlist.size(); i++) { |
077 |
PackageInfo pak = (PackageInfo) packlist.get(i); |
080 |
// 这里还可以添加系统自带的,这里就先不添加了,如果有需要可以自己添加 |
081 |
// if()里的值如果<=0则为自己装的程序,否则为系统工程自带 |
082 |
if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0 ) { |
091 |
private class baseAdapter extends BaseAdapter { |
092 |
LayoutInflater inflater = LayoutInflater.from(Apps. this ); |
095 |
public int getCount() { |
096 |
// TODO Auto-generated method stub |
101 |
public Object getItem( int position) { |
102 |
// TODO Auto-generated method stub |
107 |
public long getItemId( int position) { |
108 |
// TODO Auto-generated method stub |
113 |
public View getView( int position, View convertView, ViewGroup parent) { |
114 |
// TODO Auto-generated method stub |
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); |
126 |
holder = (ViewHolder) convertView.getTag(); |
129 |
holder.icon.setImageDrawable(list.get(position).getIcon()); |
130 |
holder.label.setText(list.get(position).getLabel().toString()); |
138 |
// 当用户点击应用程序图标时,将对这个类做出响应 |
139 |
private class ClickListener implements OnItemClickListener { |
142 |
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, |
145 |
// 将应用所选的应用程序信息共享到Application中 |
146 |
MyApp appState = ((MyApp) getApplicationContext()); |
148 |
String tab_id = appState.getTab_id(); |
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); |
160 |
// 自定义一个 AppsItemInfo 类,用来存储应用程序的相关信息 |
161 |
private class AppsItemInfo { |
163 |
private Drawable icon; // 存放图片 |
164 |
private String label; // 存放应用程序名 |
165 |
private String packageName; // 存放应用程序包名 |
167 |
public Drawable getIcon() { |
171 |
public void setIcon(Drawable icon) { |
175 |
public String getLabel() { |
179 |
public void setLabel(String label) { |
183 |
public String getPackageName() { |
187 |
public void setPackageName(String packageName) { |
188 |
this .packageName = packageName; |
在此就写完了,写得不好的地方,请高手不吝赐教
文章出处:http://blog.csdn.net/hugengzong/article/details/6366806
留言列表