【问题标题】:Close Application on Button Click [duplicate]单击按钮关闭应用程序[重复]
【发布时间】:2016-03-23 21:33:48
【问题描述】:

当我单击屏幕上的按钮时,我想关闭我的应用程序。

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();
    }
};

我做了这个活动,但是当我点击按钮时,应用程序仍然出现在最近的应用程序菜单中,我不知道为什么。

【问题讨论】:

    标签: android


    【解决方案1】:

    最好的方法是将用户带到启动器/主页。

    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

    【讨论】:

    • 但我想关闭我的应用程序。
    【解决方案2】:

    试试这个:

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

    【讨论】:

    • 它仍然出现在最近的应用程序菜单中。
    • 点击最近的应用菜单会发生什么?
    • 在要退出应用程序的按钮上保持:android.os.Process.killProcess(android.os.Process.myPid());比在清单活动标签中定义:
    • @korunos 它显示了打开的应用程序。该菜单是我可以滑动应用程序以关闭它们的地方。我的应用程序仍然出现在该菜单上,
    • 没有办法退出应用程序,因为它没有出现在最近的应用程序菜单中,因为它是系统功能!
    猜你喜欢
    • 1970-01-01
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多