2015-05-23 44 views
3

我正在嘗試創建一個啓動器,其中我添加了一個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; 
     } 
    } 
} 

回答

2

您的代碼不會有什麼是應該啓動另一個應用程序。您需要添加一些代碼來運行您在網格中顯示的應用程序。您可以爲您的mGrid實施OnItemClickListener,或爲適配器內的每個ImageView添加OnClickListener

退房從"The Big Nerd Ranch Guide"這個例子:

@Override 
public void onListItemClick(ListView l, View v, int position, long id) { 
    ResolveInfo resolveInfo = (ResolveInfo)l.getAdapter().getItem(position); 
    ActivityInfo activityInfo = resolveInfo.activityInfo; 
    if (activityInfo == null) return; 
    Intent i = new Intent(Intent.ACTION_MAIN); 
    i.setClassName(activityInfo.applicationInfo.packageName, activityInfo.name); 
    startActivity(i); 
} 

請記住,這代碼應該有ListView,而不是用GridView工作,所以一定要實現這樣的事情對你的情況。您已有一個ResolveInfo對象列表,因此您可以通過每個項目的位置使用它來訪問每個應用程序的ActivityInfo

+1

我爲GridView添加了一個OnItemClickListener,現在它的作用就像魅力! –

0

我自己找到了答案:-)只需簡單地將此代碼放在onCreate以下setContentView,即可完成!代碼就像魅力一樣!

mGrid.setOnItemClickListener(new OnItemClickListener() { 

      public void onItemClick(AdapterView<?> parent, View convertView, 
        int position, long id) { 
       ResolveInfo cleckedResolveInfo = (ResolveInfo) parent 
         .getItemAtPosition(position); 
       ActivityInfo clickedActivityInfo = cleckedResolveInfo.activityInfo; 

       Intent intent = new Intent(Intent.ACTION_MAIN); 
       intent.addCategory(Intent.CATEGORY_LAUNCHER); 
       intent.setClassName(
         clickedActivityInfo.applicationInfo.packageName, 
         clickedActivityInfo.name); 
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK 
         | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 
       startActivity(intent); 
      } 

     }); 
相關問題