2012-07-25 143 views
1

可能重複:
android - exit application code如何退出android應用程序退出按鈕?

我發現退出android應用到主屏幕(Android手機)的方式。

我的代碼並不是完全退出應用程序,而是轉向應用程序的根活動。

這裏是我的代碼

//Expression 
    Button exit = (Button) findViewById(R.id.exit); 
    exit.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      // TODO Auto-generated method stub 
      finish(); 
      System.exit(0); 
     } 
    }); 

你能告訴我如何退出應用程序並進入android手機的主屏幕?

問候,

+0

舉例來說,如果你有像A-B組2周的活動,你必須完成兩個人的退出你的應用程序。 'System.exit()'不是必需的,也是不必要的。 – 2012-07-25 05:23:56

+0

檢查您正在使用的所有活動是否正確關閉..我的意思是您應完成所有活動並從您的應用程序的根活動,然後您只需要調用finish()該活動。這將退出你的整個應用。 – 2012-07-25 05:29:22

+0

麗豐,我試過了,但還是一樣。你能給我一些線索嗎? – SopheakVirak 2012-07-25 05:30:24

回答

16
public void AppExit() 
{ 

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

    /*int pid = android.os.Process.myPid();=====> use this if you want to kill your activity. But its not a good one to do. 
    android.os.Process.killProcess(pid);*/ 

} 

調用這個方法,你點擊exit按鈕

+0

這將在後臺服務中運行應用程序。這不是一個正確的答案。 – Praveenkumar 2012-07-25 05:44:05

+0

這是我保留代碼的評論。請正確閱讀 – 2012-07-25 05:45:34

+0

請參考http://stackoverflow.com/questions/6330260/finish-all-previous-activities – JiTHiN 2012-07-25 05:51:25

相關問題