Android应用推出性能

时间:2012-07-18 12:37:06

标签: android performance android-asynctask android-activity

当我在Android模拟器上运行我的应用程序时,log-cat告诉我它跳过70帧(当应用程序开始启动时)并且它可能在主线程上做了太多。如何分散负载或提高应用程序的性能。启动时是否需要加载屏幕。

这是我的发布活动,

package com.the.maze;

import android.app.ListActivity;  
import android.content.Intent;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.ArrayAdapter;  
import android.widget.ListView;

    public class TheMazeActivity extends ListActivity{  
            String list[]={"New Game","Highscores","How to play","Settings","About"};
            Class classes[]={GameActivity.class,null,Instructions.class,null,null};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list));

    }
        @Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        try{
        Intent intent= new Intent(TheMazeActivity.this,classes[position]);
        startActivity(intent);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我建议你没有太多改进,我在你的代码中看不到繁重的计算(如果有的话!)。无论如何,这些是我的建议:

public class TheMazeActivity extends ListActivity{
    //make them static final  
    private static final String[] list={"New Game","Highscores","How to play","Settings","About"};
    private stativc final Class[] classes={GameActivity.class,null,Instructions.class,null,null};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        //no need for try catch, if you can perfectly find out, when to trigger!
        if [classes[position] != null){
            Intent intent= new Intent(TheMazeActivity.this,classes[position]);
            startActivity(intent);
        }
   }
}

但请注意,这些更改只是所谓的micro-optimization