如何从GridView中的图标启动应用程序?

时间:2015-05-23 09:40:56

标签: android

我正在尝试创建一个启动器,其中我添加了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 个答案:

答案 0 :(得分:2)

您的代码没有任何应该启动其他应用的内容。您需要添加一些代码来运行您在网格中显示的应用程序。您可以为OnItemClickListener实施mGrid,或为适配器内的每个OnClickListener添加ImageView

"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 :(得分: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);
            }

        });