为什么我的按钮在Android中反应非常缓慢?

时间:2022-11-24 09:36:17

I'm working on a launcher app, where I've created a homescreen and on my homescreen there is a app drawer button. When I press the app drawer button, it takes very long time to open up my apps activity! I don't know why this is happening, I tried on high end devices like Nexus 5, but no changes. Any solutions? Here's my code:

我正在开发一个启动器应用程序,在那里我创建了一个主屏幕,在我的主屏幕上有一个应用程序抽屉按钮。当我按下应用程序抽屉按钮时,打开我的应用程序活动需要很长时间!我不知道为什么会这样,我尝试了像Nexus 5这样的高端设备,但没有变化。有解决方案?这是我的代码:

From HomeScreen to Apps Activity Code:

从HomeScreen到Apps活动代码:

drawer.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent myIntent = new Intent(MainActivity.this, Apps.class);
                startActivity(myIntent);
                overridePendingTransition( R.anim.left, R.anim.rtl);
            }
        });

AppsActivity.java:

public class Apps extends Activity {

    SharedPreferences colors_app, color_label;
    GridView mGrid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.apps);

        loadApps();

        mGrid = (GridView) findViewById(R.id.app_grid);

        AppsAdapter adapter = new AppsAdapter(this);
        mGrid.setAdapter(adapter);

        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);

        }

        mGrid.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent,
                    View convertView, int position, long id) {
                // TODO Auto-generated method stub
                ResolveInfo cleckedResolveInfo2 = (ResolveInfo) parent
                        .getItemAtPosition(position);
                ActivityInfo clickedActivityInfo2 = cleckedResolveInfo2.activityInfo;

                Uri packageURI = Uri.parse("package:"
                        + clickedActivityInfo2.applicationInfo.packageName);
                Intent uninstallIntent = new Intent(Intent.ACTION_DELETE,
                        packageURI);
                startActivity(uninstallIntent);

                return true;
            }

        });

        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);
            }

        });
    }

    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 {

        private Context context;
        private LayoutInflater layoutInflater;

        public AppsAdapter(Context c) {
            context = c;
            layoutInflater = LayoutInflater.from(context);

        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View grid;

            if (convertView == null) {

                grid = new View(context);
                grid = layoutInflater.inflate(R.layout.grid_custom, null);

            } else {
                grid = (View) convertView;
            }

            ResolveInfo info = mApps.get(position);

            ImageView icon = (ImageView) grid.findViewById(R.id.icon);
            icon.setImageDrawable(info.activityInfo
                    .loadIcon(getPackageManager()));

            TextView label = (TextView) grid.findViewById(R.id.label);
            Typeface tf_label = Typeface.createFromAsset(getAssets(),
                    "fonts/RobotoC-Regular.ttf");
            label.setTypeface(tf_label);
            label.setText(info.activityInfo.loadLabel(getPackageManager())
                    .toString());

            color_label = getSharedPreferences("LabelColor", 1);

            int color_lab = color_label.getInt("color_code_label", 0);
            if (color_lab != 0) {
                label.setTextColor(color_lab);

            }

            return grid;
        }

        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;
        }
    }
}

Nobodys got answer?

Nobodys得到了答案?

1 个解决方案

#1


0  

Profiling the performance of your code will let you see where the bottleneck is.

分析代码的性能可以让您了解瓶颈的位置。

I would suggest it will be in the getView() function:

我建议它将在getView()函数中:

(1) The function call to loadLabel is known to potentially be slow.

(1)已知对loadLabel的函数调用可能很慢。

PackageInfo LoadLabel slow performance

PackageInfo LoadLabel性能缓慢

(2) Calling Typeface.createFromAsset for every item in the adapter is not a good idea.

(2)为适配器中的每个项调用Typeface.createFromAsset不是一个好主意。

#1


0  

Profiling the performance of your code will let you see where the bottleneck is.

分析代码的性能可以让您了解瓶颈的位置。

I would suggest it will be in the getView() function:

我建议它将在getView()函数中:

(1) The function call to loadLabel is known to potentially be slow.

(1)已知对loadLabel的函数调用可能很慢。

PackageInfo LoadLabel slow performance

PackageInfo LoadLabel性能缓慢

(2) Calling Typeface.createFromAsset for every item in the adapter is not a good idea.

(2)为适配器中的每个项调用Typeface.createFromAsset不是一个好主意。