所有活动中的通用对话框

时间:2015-12-20 10:19:08

标签: android dialog android-dialog

我想对所有活动进行网络检查,这样如果没有可用的网络,将出现一个对话框,并且必须完成当前活动。

为此,我创建了一个ConnectionDetector文件来检查连接 在这个java文件中,我创建了一个对话框 当我单击对话框按钮时,必须完成调用Activity。

如何将当前活动作为参数发送?

我的活动

if (!ConnectionDetector.isConnectingToInternet()) {
       ConnectionDetector.noConnectionDialog(getApplicationContext(), this);
}

ConnectionDetector.java

public class ConnectionDetector {

    private Context _context;

    public ConnectionDetector(Context context) {
        this._context = context;
    }

    public static boolean isConnectingToInternet() {
        ConnectivityManager connectivity = (ConnectivityManager) AppController.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null)
                for (int i = 0; i < info.length; i++)
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }

        }
        return false;
    }

    public static void noConnectionDialog(final Context context, final Activity activity) {
        final AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle("Network error");
        dialog.setMessage("There seems to be a connection problem. Please check your network connection and try again");
        dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                activity.finish();
            }
        });
        dialog.show();
    }
}

获取空指针异常,活动未完成。

3 个答案:

答案 0 :(得分:2)

如果您总是要使用show this dialog form activity,也可以

public static void noConnectionDialog(final Context context) {
        final AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle("Network error");
        dialog.setMessage("There seems to be a connection problem. Please check your network connection and try again");
        dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {                
                 dialog.dismiss();
                 ((Activity)context).finish();
            }
        });
        dialog.show();
    }

答案 1 :(得分:2)

通过使用自定义活动,我们可以实现这一目标。

答案 2 :(得分:0)

public static void noConnectionDialog(final Context context, String c) {

        Dialog dialog = new Dialog(context);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.nointernet);
        Window window = dialog.getWindow();
        window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);

        ImageButton close;
        close = dialog.findViewById(R.id.close);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        if (c.equals("1")) {
            dialog.show();
        } else {
            dialog.dismiss();
        }
    }