我正在嘗試創建一個啓動器,其中我添加了一個GridView
。這將獲取所有已安裝的應用程序並將其排列在網格中。如何從GridView中的圖標啓動應用程序?
問題是當我點擊這些網格項時什麼也沒有發生,我希望它打開應用程序。
可能嗎?這裏是我的代碼:
Apps.java:
public class Apps extends Activity {
SharedPreferences colors_app;
GridView mGrid;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
loadApps();
setContentView(R.layout.apps);
mGrid = (GridView) findViewById(R.id.app_grid);
mGrid.setAdapter(new AppsAdapter());
colors_app = getSharedPreferences("MyColor", 1);
int colorcode2 = colors_app.getInt("color_code", 0);
if (colorcode2 != 0) {
Apps.this.findViewById(R.id.apps_back).setBackgroundColor(
colorcode2);
}
}
private List<ResolveInfo> mApps;
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 i;
if (convertView == null) {
i = new ImageView(Apps.this);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new GridView.LayoutParams(90, 90));
i.setPadding(10, 10, 10, 10);
} 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;
}
}
}
我爲GridView添加了一個OnItemClickListener,現在它的作用就像魅力! –