사용자가 응용 프로그램을 종료 한 후 진동과 같은 모든 작업을 중지해야합니다. 어떻게해야합니까? 내 앱은 사용자가 선택한 특정 시간 동안 전화를 진동하지만 사용자가 시작하고 앱을 종료하면 휴대 전화가 선택한 시간 동안 계속 진동합니다. 어떻게이 오류를 해결할 수 있습니까? 모든사용자가 응용 프로그램을 나갈 때 완료로 실행을 완료 할 수 없습니다.
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();
}
});
}
}
OnPause() 및 Onstop()을 사용한 후 앱을 종료하면 진동이 종료됩니다 ...하지만 휴대 전화를 종료하고 차단하면 휴대 전화를 차단 해제하고 진동기를 사용하고, 그리고 전화기는 내가 마지막으로 진동을 선택했을 때를 기억하고 ... 시작합니다! 바이브레이터를 끝내는 방법 (그리고 버퍼?) 완전히 ?? – Rcgoncalves