不必要的跳帧错误

时间:2016-07-05 10:46:04

标签: android multithreading performance android-asynctask

我收到了不必要的跳过消息。应用程序在其主线程中做了太多工作。在阅读了几个答案后,我意识到要解决它,我必须使用多线程。但我的代码非常基本。尽管如此,我还是通过使用Async Class在后台完成工作来重写代码。

我附加到活动文件的代码。首先是启动画面,另一个是启动画面的结果。即使在我的手机屏幕上,我也会遇到不必要的延迟。

这是启动画面的代码

package bt4u.com.pokemonbattles;
public class splash extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        System.out.println("Splash screen activity (splash.java)");
        new myClass().execute();
}

class myClass extends AsyncTask<Void,Void,Void> {
    @Override
    protected Void doInBackground(Void... params) {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        Intent intent=new Intent(splash.this,gameOptions.class);
        startActivity(intent);
        finish();
    }
}

}

这是结果活动之后的

public class gameOptions extends AppCompatActivity {

int gameTypeCons=0,gameFieldCons=0;
int difficulty=0; // 0-easy, 1-medium, 2-hard
int cpu=1;//0-player 1 is cpu, 1- player 2 is cpu
Button newGame;
Spinner gameType,gameField,p1,p2;
String[] options={"2 Players Local","Player vs CPU","2 Players Online","Bluetooth Play","Wi-FI Play"}, field={"3X3","4X4"},
        pl={"Chikorita","Pikachu","Bulbasaur","Charmender","Mewto","Lapras","Onix","Squirtle","Dragonite","Charizard"}
        ,pl2={"Squirtle","Charizard","Dragonite","Onix","Chikorita","Lapras","Mewto","Charmender","Bulbasaur","Pikachu"};
String p1poke, p2poke,player1Name, player2Name;

EditText name1,name2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game_options);
    System.out.println("Game options activity (gameoptions.class)");

    gameType=(Spinner)findViewById(R.id.gameType);
    gameField=(Spinner)findViewById(R.id.gameField);
    p1=(Spinner)findViewById(R.id.p1);
    p2=(Spinner)findViewById(R.id.p2);
    newGame = (Button)findViewById(R.id.newGame);
    name1 = (EditText)findViewById(R.id.editText);
    name2 = (EditText)findViewById(R.id.editText2);

    ArrayAdapter optionsAdapter = new ArrayAdapter(gameOptions.this,android.R.layout.simple_dropdown_item_1line,options);
    optionsAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    gameType.setAdapter(optionsAdapter);

    ArrayAdapter fieldAdapter = new ArrayAdapter(gameOptions.this,android.R.layout.simple_dropdown_item_1line,field);
    fieldAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    gameField.setAdapter(fieldAdapter);

    ArrayAdapter player1 = new ArrayAdapter(gameOptions.this,android.R.layout.simple_dropdown_item_1line,pl);
    player1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    p1.setAdapter(player1);

    ArrayAdapter player2 = new ArrayAdapter(gameOptions.this,android.R.layout.simple_dropdown_item_1line,pl2);
    player2.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
    p2.setAdapter(player2);

    gameType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            gameTypeCons=position;
            if (position==1){
                showDialog(1);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    gameField.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            gameFieldCons=position;
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    p2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            p2poke=pl2[position];
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    p1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            p1poke=pl[position];
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

    newGame.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            player1Name=name1.getText().toString();
            player2Name=name2.getText().toString();
            new myClass().execute();

        }
    });


}
class task2 extends AsyncTask<Void,Void,Void>{
    @Override
    protected Void doInBackground(Void... params) {
        return null;
    }
}


public Dialog onCreateDialog(int id){
    Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.cpucustomdialog);

    RadioGroup radioGroup = (RadioGroup)dialog.findViewById(R.id.diff);
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId==R.id.radioButton)
                difficulty=0;
            else if (checkedId==R.id.radioButton2)
                difficulty=1;
            else if (checkedId==R.id.radioButton3)
                difficulty=2;
            else
                difficulty=0;
        }
    });

    RadioGroup radioGroup2 = (RadioGroup)dialog.findViewById(R.id.cpu);
    radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if (checkedId==R.id.radioButton4)
                cpu=0;
            else
                cpu=1;
        }
    });

    dialog.show();
    return dialog;
}

class myClass extends AsyncTask<Void,Void,Void>{

    @Override
    protected Void doInBackground(Void... params) {

        SharedPreferences sp = getSharedPreferences("prefs", 0);
        SharedPreferences.Editor editor = sp.edit();


        editor.putString("player1Name", player1Name);
        editor.putString("player2Name", player2Name);
        editor.putString("player1pokemon",p1poke);
        editor.putString("player2Pokemon",p2poke);
        editor.putInt("difficulty",difficulty);
        editor.putInt("cpu",cpu);
        editor.commit();
        if (gameFieldCons==0){
            switch (gameTypeCons){
                case 0:
                    Intent intent = new Intent(gameOptions.this,MainActivity.class);
                    startActivity(intent);
                    break;
                case 1:
                    if (cpu==1){
                        Intent intent2 = new Intent(gameOptions.this,cpueasy.class);
                        startActivity(intent2);
                    }
                    else{
                        Intent intent2 = new Intent(gameOptions.this,demo.class);
                        startActivity(intent2);
                    }
                    break;
                default:
                    break;
            }
        }
        else {
            switch (gameTypeCons){
                case 0:
                    Intent intent = new Intent(gameOptions.this,bigmain.class);
                    startActivity(intent);
                    break;
                case 1:
                    if (cpu==1) {
                        Intent intent2 = new Intent(gameOptions.this, igcpu.class);
                        startActivity(intent2);
                    }
                    else{
                        Intent intent2 = new Intent(gameOptions.this,demo2.class);
                        startActivity(intent2);
                    }
                    break;
                default:
                    break;
            }
        }
        return null;
    }
}
}

这是启动画面启动和启动画面结果之间的logcat。

I/System.out: Splash screen activity (splash.java)
I/Choreographer: Skipped 43 frames!  The application may be doing too much work on its main thread.
I/Choreographer: Skipped 44 frames!  The application may be doing too much work on its main thread.
I/Choreographer: Skipped 34 frames!  The application may be doing too much work on its main thread.
I/Choreographer: Skipped 38 frames!  The application may be doing too much work on its main thread.
I/Timeline: Timeline: Activity_launch_request id:bt4u.com.pokemonbattles time:415126775
I/Choreographer: Skipped 41 frames!  The application may be doing too much work on its main thread.
I/Choreographer: Skipped 37 frames!  The application may be doing too much work on its main thread.
I/System.out: Game options activity (gameoptions.class)
I/Choreographer: Skipped 75 frames!  The application may be doing too much work on its main thread.
I/Choreographer: Skipped 54 frames!  The application may be doing too much work on its main thread.
I/Choreographer: Skipped 80frames!  The application may be doing too much work on its main thread.
I/Choreographer: Skipped 74 frames!  The application may be doing too much work on its main thread.
I/Choreographer: Skipped 33frames!  The application may be doing too much work on its main thread.
I/Choreographer: Skipped 34 frames!  The application may be doing too much work on its main thread.
I/Choreographer: Skipped 32 frames!  The application may be doing too much work on its main thread.
I/Choreographer: Skipped 36 frames!  The application may be doing too much work on its main thread.
I/Choreographer: Skipped 32 frames!  The application may be doing too much work on its main thread.
I/Choreographer: Skipped 30 frames!  The application may be doing too much work on its main thread.
W/art: Suspending all threads took: 12.592ms
W/InputEventReceiver: Attempted to finish an input event but the input event receiver has already been disposed.
W/InputMethodManager: startInputInner : InputBindResult == null
W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection

1 个答案:

答案 0 :(得分:1)

只需为您的启动画面执行以下代码,将splash.java替换为以下文件:

public class splash extends Activity {

// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    new Handler().postDelayed(new Runnable() {

        /*
         * Showing splash screen with a timer. This will be useful when you
         * want to show case your app logo / company
         */

        @Override
        public void run() {
            // This method will be executed once the timer is over
            // Start your app main activity
            Intent i = new Intent(splash.this, gameOptions.class);
            startActivity(i);

            // close this activity
            finish();
        }
    }, SPLASH_TIME_OUT);
}

}