2015-12-17 152 views
-3

我想在我單擊屏幕上的按鈕時關閉我的應用程序。關閉按鈕應用程序點擊

public WindowManager winManager; 
public RelativeLayout wrapperView; 
public Button button1; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    WindowManager.LayoutParams localLayoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR, 
      WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, 
      PixelFormat.TRANSLUCENT); 
    this.winManager = ((WindowManager)getApplicationContext().getSystemService(WINDOW_SERVICE)); 
    this.wrapperView = new RelativeLayout(getBaseContext()); 
    getWindow().setAttributes(localLayoutParams); 
    View.inflate(this, R.layout.activity_main, this.wrapperView); 
    this.winManager.addView(this.wrapperView, localLayoutParams); 
    button1 = (Button)wrapperView.findViewById(R.id.button1); 
    button1.setOnClickListener(mButton1_OnClickListener); 
} 

View.OnClickListener mButton1_OnClickListener = new View.OnClickListener() { 
    public void onClick(final View v){ 
       winManager.removeView(wrapperView); 
       wrapperView.removeAllViews(); 
       finish(); 
    } 
}; 

我做了這個活動,但是當我點擊按鈕的應用程序仍然出現在最近的應用程序菜單上,我不知道爲什麼。

回答

0

試試這個:

@Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     finish(); 
     System.exit(0); 
    } 
+0

它仍然出現在最近的應用程序菜單上。 – tomss

+0

如果您點擊最近的應用菜單,會發生什麼情況? – korunos

+0

在要退出應用程序的按鈕上保留: android.os.Process.killProcess(android.os.Process.myPid()); 比定義清單活動標籤: <活動 機器人:名字=「你的活動」 機器人:excludeFromRecents =「真」 – santoXme

0

最好的辦法是採取用戶啓動/主頁。

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

參考這樣的:https://stackoverflow.com/a/3226743/1283821

+0

但我想關閉我的應用程序。 – tomss