2014-11-25 34 views
1

我有一個Android應用程序..我想whrn用戶點擊退出按鈕它會顯示確認消息,如果他點擊是關閉整個應用程序..的onClick關閉應用程序

嘗試這種代碼,但沒「T工作:

AlertDialog.Builder alertBuilder=new AlertDialog.Builder(AdminEditProfile.this); 
alertBuilder.setTitle("Exit"); 
alertBuilder.setMessage("Are you sure you want to exit from the application?"); 
alertBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() { 

    public void onClick(DialogInterface dialog, int which) { 

     dialog.cancel(); 
     android.os.Process.killProcess(android.os.Process.myPid()); 
     System.exit(1); 
    } 
}); 
alertBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() { 

    public void onClick(DialogInterface dialog, int which) { 

     dialog.cancel();  
    } 
}); 
alertBuilder.create().show(); 

是有辦法做到這一點>

請幫忙,謝謝:)。

+0

張貼您日誌貓錯誤。 – Yugesh 2014-11-25 07:55:04

+1

這是一個**糟糕的做法**強行關閉你的應用程序。改爲使用「主頁」和「後退」按鈕。 – 2014-11-25 07:56:57

+1

這就是爲什麼它是不好的做法:http://android.nextapp.com/site/fx/doc/exit – 2014-11-25 07:58:08

回答

-1

我的示例代碼試試這個

new AlertDialog.Builder(MainActivity.this) 
     .setTitle("Application Informaiton") 
     .setMessage("") 
     .setPositiveButton("Got it", new 
     DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
     // continue with delete 
     } 
     }) 
     .setCancelable(false) 
     .show(); 

     } 
     else if(item.getTitle().equals("Stop this Application")) 
     { 
      new AlertDialog.Builder(MainActivity.this) 
      .setTitle("Stop application?") 
      .setMessage("Stop this application and exit? You'll need to 
      re-launch the application to use it again.") 
      .setPositiveButton("Don't stop", new 
      DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
      // continue with delete 
      } 
      }) 
      .setNegativeButton("Stop and exit", new 
       DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
       // continue with delete 
       MainActivity.this.finish(); 
       Intent intent = new Intent(Intent.ACTION_MAIN); 
       intent.addCategory(Intent.CATEGORY_HOME); 
       intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
            startActivity(intent); 
           } 
          }) 
          .setCancelable(false) 
          .show(); 
+0

工作!非常感謝 !! – tatwany 2014-11-26 12:54:24

+0

歡迎您! – krishnan 2014-11-26 14:39:11

2

剛剛完成();可以做關閉Activity的工作。有沒有任何服務在後臺運行?

+0

finish();將關閉當前活動並將我返回到前一個..我想關閉整個應用程序..有沒有辦法? – tatwany 2014-11-26 12:35:10

+0

檢查http://stackoverflow.com/questions/14001963/finish-all-activities-at-a-time?answertab=oldest#tab-top – 2014-11-26 12:42:53

+0

感謝重播..我找到了正確的答案:) – tatwany 2014-11-26 12:57:01

1

如果它是一個簡單的應用程序,你可以撥打finish()並關閉你所有打開的活動。 也許你應該從應用中註銷用戶?

+0

完成();將關閉當前活動並將我返回到前一個..我想關閉整個應用程序..有沒有辦法? – tatwany 2014-11-26 12:36:11

+0

使用broadcastreceiver並註冊您的所有活動的具體行動 - 然後關閉所有活動,獲取此信息 – Unii 2014-11-26 12:49:27

+0

感謝重播..我找到了正確的答案:) – tatwany 2014-11-26 12:56:35

3

你正在扼殺你的過程和那個壞主意。 請看看這篇文章,找出了原因:

Why calling Process.killProcess(Process.myPid()) is a bad idea?

我建議你只使用finish();代替。

如果你想完成整個應用程序(取決於你所需要究竟是什麼),我建議你看到這個帖子:

How to close Android application?

+0

finish();將關閉當前活動並將我返回到前一個..我想關閉整個應用程序..有沒有辦法? – tatwany 2014-11-26 12:35:42

+0

@tatwany我更新了我的回答 – 2014-11-27 06:32:53

+0

非常感謝您的回覆,actullay我找到了答案,您發佈的第二個鏈接是這篇文章,這是我的:) – tatwany 2014-11-27 10:40:43