java - 当用户离开应用程序时无法通过完成完成执行

标签 java android quit

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

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

最佳答案

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

例如,覆盖

protected void onPause() {}

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

同样,如果需要,您可以实现onStoponDestroy。但在你的情况下,我相信 onStoponPause 就足够了:)

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

关于java - 当用户离开应用程序时无法通过完成完成执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16169454/

相关文章:

尝试更新 XML 中的值时出现 javax.xml.xpath.XPathExpressionException

android - Amazon Silk Browser - 移动 Web 开发人员的注意事项

java - 从 SD 卡获取图像时出现 FileNotFoundException

cocoa - 如何区分 Cocoa 中应用程序终止的不同原因?

java - 安卓/ArduinoBT连接

java - 如何在 Java 中声明大字符串(<Html> 代码)以避免 "Constant string too long"错误?

java - 如何用不同的子字符串替换多个子字符串?

android - 在 Android 应用程序中从图像构造 3D 几何图形

c++ - 如果满足条件,如何退出 Qt 脚本?

java - 使用 System.exit(0) 强制退出 Android 应用程序不起作用