1

ユーザーがアプリケーションを終了した後、アプリが実行していること (振動など) をすべて停止する必要があります。私のアプリは、ユーザーが選択した一定時間電話を振動させますが、ユーザーがアプリを起動して終了すると、選択した時間だけ携帯電話が振動し続けます..このエラーをどのように処理できますか?

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

3 に答える 3

1

まず、アプリケーションの終了と一時停止を区別する必要があります (別のアプリケーションがフォアグラウンドになると、一時停止が発生します)。次に、アプリケーションが一時停止または破棄されたときに何が起こるかを処理するために、適切なメソッドをオーバーライドする必要があります。

たとえば、オーバーライド

protected void onPause() {}

アプリケーションが一時停止されたときに何が起こるべきかを定義できるため、アプリケーションが実行しているものは何でも適切に停止できます。

同様に、必要に応じonStopて andを実装できonDestroyます。しかし、あなたの場合、私はそれで十分だonStopと信じています:)onPause

また、このページを見てみてください。アクティビティのライフサイクルの詳細な説明が記載されています http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

于 2013-04-23T12:43:20.390 に答える
0

アクティビティのバイブレーション サービスを停止する必要がonStop()あります。

@Override
protected void onStop() {
            Vibrator rr = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            rr.cancel();
     }
于 2013-04-23T12:45:03.753 に答える
0

あなたの活動に追加onPauseし、そこからバイブレーターをキャンセルします:

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

これにより、アクティビティがフォアグラウンドになり、別のアクティビティが発生したときにバイブレーターが停止します (たとえば、アクティビティがフォアグラウンドにあるときに電話がかかってきた場合など)。これは、アプリの終了時にのみバイブレーターをキャンセルするよりも望ましい動作です。

于 2013-04-23T12:45:25.580 に答える