android当我最小化应用程序它被破坏?

时间:2017-07-13 09:55:23

标签: android android-activity

android:当我最小化app时它会被破坏,当我再次将它取回时,它会在打开之前打开重新打开的表单 这是我的代码:

    public class tem2 extends AppCompatActivity {
    private MediaPlayer objPlayer ,objPlayer2 ;
    private TextToSpeech textToSpeech;
    private String[] counter;
    private String show_b = "", show_r = "";
    private String new_name_team_one, new_name_team_two, score, toSpeak;
    private TextView f_red_val, f_blue_val, blue_name, red_name, sc, team_blue1, team_red1;
    private EditText red_val, blue_val;
    private int[] before;
    private int result, result1;
    private AlertDialog.Builder builder;

    private int r;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tr);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
 try {
            textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(int status) {
                    if (status != TextToSpeech.ERROR) {
                        textToSpeech.setLanguage(Locale.UK);
                    }
                }
            });
            objPlayer2 = MediaPlayer.create(this, R.raw.error);
            objPlayer = MediaPlayer.create(this, R.raw.victor);
            blue_name = (TextView) findViewById(R.id.blue);
            red_name = (TextView) findViewById(R.id.red);
            sc = (TextView) findViewById(R.id.score_text);
            team_blue1 = (TextView) findViewById(R.id.team_blue1);
            team_red1 = (TextView) findViewById(R.id.team_red1);

            // objPlayer = MediaPlayer.create(this, R.raw.victor);
            //objPlayer2 = MediaPlayer.create(this, R.raw.error);

            textToSpeech = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
                @Override
                public void onInit(int status) {
                    if (status != TextToSpeech.ERROR) {
                        textToSpeech.setLanguage(Locale.UK);
                    }
                }
            });


            f_red_val = (TextView) findViewById(R.id.team_red);
            f_blue_val = (TextView) findViewById(R.id.team_blue);
            red_val = (EditText) findViewById(R.id.red_val);
            blue_val = (EditText) findViewById(R.id.blue_val);
            Intent i = getIntent();
            new_name_team_one = i.getStringExtra("team_one_name");
            blue_name.setText(new_name_team_one);
            new_name_team_two = i.getStringExtra("team_two_name");
            red_name.setText(new_name_team_two);
            score = i.getStringExtra("score");
            r = Integer.parseInt(score);
            sc.setText("Score  " + score);
            builder = new AlertDialog.Builder(this);
            builder.setTitle(" THE Winner ");
            builder.setPositiveButton("cancel", null);
            builder.setCancelable(false);
            counter = new String[1];
            before = new int[1];
            before[0] = 0;
        }catch (Exception e){

        }



    }

    public void apply() {
        f_blue_val.setText("");
        f_red_val.setText("");
        red_val.setText("");
        blue_val.setText("");
        team_blue1.setText("");
        team_red1.setText("");
        show_r="";
        show_b="";


    }

    public void play_sound(String str) {
        toSpeak = "The winner is Team " + str;
        objPlayer.start();
        textToSpeech.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);

    }

    public void add_B(View view) {
        try {
            //   Toast.makeText(tem2.this, "blue", Toast.LENGTH_LONG).show();
            counter[0] = "blue";
            String res = f_blue_val.getText().toString();
            String res_blue = blue_val.getText().toString();

            if (res == "") {
                res = "0";
            }
            if (res_blue == "") {
                res_blue = "0";
            }
            result = Integer.parseInt(res) + Integer.parseInt(res_blue);
            if (show_b == "") {

                show_b = res_blue;
            } else {
                show_b = show_b + "\n" + res_blue;

            }
            team_blue1.setText(show_b);

            before[0] = Integer.parseInt(res_blue);
            f_blue_val.setText("" + result);
            blue_val.setText("");
            String ss = new_name_team_one;

            if (result >= r) {
                play_sound(new_name_team_one);
                builder.setMessage(ss + "");
                builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        apply();
                        before[0] = 0;

                    }
                });
                builder.create().show();
            }

        } catch (Exception e) {
            Toast.makeText(tem2.this, e.toString(), Toast.LENGTH_LONG).show();

        }
    }

    public void add_R(View view) {
        try {
            //Toast.makeText(tem2.this, "red", Toast.LENGTH_LONG).show();

            counter[0] = "red";
            String res1 = f_red_val.getText().toString();
            String res1_red = red_val.getText().toString();
            try {
                Integer.parseInt(res1_red);
            } catch (Exception e) {
                objPlayer2.start();

            }
            if (res1 == "") {
                res1 = "0";
            }
            if (res1_red == "") {
                res1_red = "0";
            }
            result1 = Integer.parseInt(res1) + Integer.parseInt(res1_red);
            if (show_r == "") {
              show_r=res1_red ;

            } else {
                show_r=team_red1.getText() + "\n" + res1_red;


            }
            team_red1.setText(show_r);
            /////////////////////////
            before[0] = Integer.parseInt(res1_red);
            f_red_val.setText("" + result1);
            red_val.setText("");
            String ff = new_name_team_two;
            if (result1 >= r) {
                builder.setMessage(ff + " ");
                play_sound(new_name_team_two);
                builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        apply();
                        before[0] = 0;

                    }
                });
                builder.create().show();
            }

        } catch (Exception e) {
          //  Toast.makeText(tem2.this, e.toString(), Toast.LENGTH_LONG).show();
            Toast.makeText(tem2.this, "Error", Toast.LENGTH_LONG).show();

        }
    }

    public void undo(View view) {
        try {
            String c = counter[0];
            if (c == "red") {
                String res1 = f_red_val.getText().toString();
                int back_result_red = Integer.parseInt(res1) - before[0];
                f_red_val.setText("" + back_result_red);
                if (show_r == "") {
                    show_r = "";
                } else {
                    if (before[0] != 0) {
                        show_r = team_red1.getText() + "\n" + "-" + before[0];
                    }
                }
                team_red1.setText(show_r);
            } else {
                String res = f_blue_val.getText().toString();
                int back_result_blue = Integer.parseInt(res) - before[0];
                f_blue_val.setText("" + back_result_blue);
                if (show_b == "") {
                    show_b = "";
                } else {
                    if (before[0] != 0) {
                        show_b = team_blue1.getText() + "\n" + "-" + before[0];
                    }
                }
                team_blue1.setText(show_b);
            }
            before[0] = 0;

    }catch (Exception e){

    }
}
    @Override
    public void onResume() {
        super.onResume();  // Always call the superclass method first

        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
        wl.acquire();

// screen and CPU will stay awake during this section

      //  wl.release();

    }

    @Override
    protected void onStart() {

        super.onStart();

    }

    @Override
    public void onPause() {
        super.onPause();  // Always call the superclass method first

        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
        // wl.acquire();

// screen and CPU will stay awake during this section

        wl.release();
    }
    @Override
    public void onDestroy() {

        super.onDestroy();
    }
}

我的应用程序在名称(多米诺计算器)here

的游戏商店中

1 个答案:

答案 0 :(得分:0)

 you are using MediaPlayer objPlayer,objPlayer2 and you have start like
 this objPlayer2.start(); but when you are minimize the app onPause() method
 called and you have to stop your mediaplayer like this.
 @Override
 public void onPause(){
    super.onPause();  //Always call the superclass method first
    objPlayer.pause();
    objPlayer2.pause();
    PowerManager pm = (PowerManager)
    getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl =
    pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
    // wl.acquire();
    // screen and CPU will stay awake during this section
    wl.release();
  }