【问题标题】:Can't finish execution by complete when user leave the app用户离开应用程序时无法完成执行
【发布时间】:2013-04-23 12:32:35
【问题描述】:

在用户退出应用程序后,我需要停止我的应用程序正在执行的所有操作(例如振动),我该怎么做?我的应用程序在用户选择的一定时间内振动手机,但如果用户启动并退出应用程序..手机继续振动所选时间..我该如何处理这个错误?

public class MainActivity extends Activity {
    EditText tempo;
    Button bt;
    Thread t;
    int estado = 1;

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

        tempo = (EditText) findViewById(R.id.tempo);
        //long delay = Long.parseLong(tempo.getText().toString());

        bt = (Button) findViewById(R.id.btvibrar);

        bt.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {

                if (!tempo.getText().toString().equals("")) {

                    if (estado == 1) {

                        Vibrar();
                        estado *= -1;

                        bt.setText("Parar !");
                        bt.setBackgroundColor(Color.RED);

                        //Handler handler = new Handler();
                        //handler.postDelayed(new Runnable() {

                        //@Override
                        //public void run() {
                        //estado*=-1;
                        //bt.setText("Vibrar !");
                        //bt.setBackgroundColor(Color.GREEN);
                        //}
                        //  },  );
                    } else {
                        Parar();
                        estado *= -1;
                        bt.setText("Vibrar !");
                        bt.setBackgroundColor(Color.GREEN);
                   }
                } else {
                    AlertDialog.Builder dialogo = new AlertDialog.Builder(MainActivity.this);
                    dialogo.setTitle("Erro !");
                    dialogo.setMessage("Escolha um tempo.");
                    dialogo.setNeutralButton("OK", null);
                    dialogo.show();

                }
            }

            private void Vibrar() {    // É necessario lançar excessao no ANDROIDMANIFEST.XML
                Vibrator rr = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                long treal = Long.parseLong(tempo.getText().toString());
                long milliseconds = treal * 1000;
                rr.vibrate(milliseconds);
            }

            private void Parar() {
                Vibrator rr = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                rr.cancel();
            }
        });
    }
}

【问题讨论】:

    标签: java android quit


    【解决方案1】:

    首先,您需要区分退出和暂停应用程序(如果另一个应用程序进入前台,则会发生暂停)。其次,您需要重写适当的方法来处理应用程序暂停或销毁时发生的情况。

    例如,覆盖

    protected void onPause() {}
    

    将允许您定义应用程序暂停时应该发生的情况,因此,您可以优雅地停止应用程序正在执行的任何操作。

    同样,如果需要,您可以实现 onStoponDestroy。但就你而言,我相信onStoponPause 就足够了:)

    另外,试着看看这个页面,它给出了活动生命周期的详细描述 http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

    【讨论】:

    • 找到一个“Bug”,使用OnPause()Onstop()并退出应用程序后,振动结束...但是当我退出并屏蔽我的手机时,使用振动器解锁手机,然后手机“记住”我最后一次选择振动...并开始!如何完全结束振动器(和缓冲器?)??
    【解决方案2】:

    您需要在活动的onStop()停止振动服务。

    @Override
    protected void onStop() {
                Vibrator rr = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                rr.cancel();
         }
    

    【讨论】:

      【解决方案3】:

      onPause 添加到您的活动中并从那里取消振动器:

      @Override
      public void onPause() {
          Parar();
      }
      

      当您的活动进入前台并且另一个活动出现时(例如,当您的活动处于前台时有来电),这将停止振动器。这可能是比仅在应用完成时取消振动器更理想的行为。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-25
        • 1970-01-01
        • 2010-09-17
        • 2021-04-19
        相关资源
        最近更新 更多