2013-04-23 3 views
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(); 
      } 
     }); 
    } 
} 

답변

1

먼저, 그만 둘을 구별해야하고, 응용 프로그램을 (다른 응용 프로그램이 포 그라운드로 오는 경우 일시 정지가 발생) 일시 중지. 둘째, 애플리케이션을 일시 중지하거나 삭제할 때 발생하는 상황을 처리하기 위해 적절한 메소드를 재정의해야합니다. 응용 프로그램이 일시 정지 될 때, 그래서, 당신이 적절하게 응용 프로그램이하고있는 어떤 중지 할 수 있습니다 어떻게해야하는지 정의 할 수

protected void onPause() {} 

를 오버라이드 (override) 예를 들어

.

마찬가지로, 필요한 경우 onStoponDestroy을 구현할 수 있습니다. 그러나 귀하의 경우에, 나는 onStoponPause이 :)

또한 충분이 페이지를 봐주는 시도 할 것으로 예상, 그것은 http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

+0

OnPause() 및 Onstop()을 사용한 후 앱을 종료하면 진동이 종료됩니다 ...하지만 휴대 전화를 종료하고 차단하면 휴대 전화를 차단 해제하고 진동기를 사용하고, 그리고 전화기는 내가 마지막으로 진동을 선택했을 때를 기억하고 ... 시작합니다! 바이브레이터를 끝내는 방법 (그리고 버퍼?) 완전히 ?? – Rcgoncalves

0

당신은 진동 서비스를 중지하는 데 필요한 활동의 ​​라이프 사이클에 대한 자세한 설명을 제공합니다 너의 활동 중 onStop().

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

당신의 ativity에 onPause을 추가하고 취소 진동을 거기에서 : 당신의 활동이 포 그라운드로 이동하고 다른 활동이 나오면 동안

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

이 (예를 들어 수신 전화를 진동을 중지됩니다 활동이 전경에 있음). 이것은 앱이 끝날 때만 진동기를 취소하는 것보다 바람직한 행동 일 수 있습니다.