【发布时间】:2014-08-25 14:53:22
【问题描述】:
我写了 alertdialog 并尝试在确定单击时退出应用程序,我写了一些代码但是当我单击确定按钮时应用程序会自动重新启动 这是我的来源
public class AlertDialogManager {
public void showAlertDialog(Context context, String message) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setMessage(message);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
});
alertDialog.show();
}
我像这样调用这个类,但我无法在确定点击中关闭应用程序
if (!con.isConnectingToInternet()) {
alert.showAlertDialog(getActivity(),
"You have not internet connection");
} else {
mainthread.execute();
}
P.s 我检查了互联网连接。它工作正常。别担心 :)
我做错了什么?如果有人知道解决方案,请帮助我
【问题讨论】:
-
除非您需要停止后台进程,否则不要在 Android 应用中退出。这很烦人,不需要。
-
Process.killProcess(android.os.Process.myPid());和System.exit(1);是您可以对您的应用程序做的最糟糕的事情之一。这不仅会退出您的应用程序,还会使您的应用程序崩溃。 **永远不要调用这些方法,只需使用finish()。 Android 操作系统决定何时启动/停止应用程序。 -
如果你想退出错误,然后抛出一个RuntimeException。
-
所以显示警报但为什么要退出?它不需要而且很糟糕。
标签: android eclipse android-activity