2014-07-01 3 views
-1

다음 코드를 사용하여 애플리케이션을 종료합니다.알림 대화 상자가있는 Android 종료 버튼

Intent intent = new Intent(Intent.ACTION_MAIN); 
       intent.addCategory(Intent.CATEGORY_HOME); 
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       startActivity(intent); 

이제 종료를 누르면 경고 대화 상자가 표시됩니다. 내가 확인을 누르면, 그렇지 않으면 응용 프로그램을 닫습니다.

+2

특정 문제가 무엇입니까? – JoxTraex

+1

그런 다음 alertdialog에 코드를 작성하십시오. 문제는 무엇입니까? – Ranjit

+0

메뉴 모음에서 하나의 아이콘으로 나가기 옵션을 사용했습니다. 어떻게 알림을 가져올 수 있습니까? – jana

답변

8

는 super.OnBackPressed를 제거()

@Override 
public void onBackPressed() { 
    AlertDialog.Builder builder = new AlertDialog.Builder(this); 
    builder.setMessage("Are you sure you want to exit?") 
      .setCancelable(false) 
      .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        MyActivity.this.finish(); 
       } 
      }) 
      .setNegativeButton("No", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        dialog.cancel(); 
       } 
      }); 
    AlertDialog alert = builder.create(); 
    alert.show(); 

} 
+0

Nepster에게 감사드립니다. 이것은 내가 원하는 것입니다. – jana

0

this link.. 로부터 긍정적 인 버튼의 총에 AlertDialog 코드를 같이 쓰기 체크 :

.setPositiveButton("Logout",new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog,int id) { 
    //your_activity.finish();......(1) 
      //otherwise use your code.. 
      Intent intent = new Intent(Intent.ACTION_MAIN); 
      intent.addCategory(Intent.CATEGORY_HOME); 
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      startActivity(intent); 

    } 
});