如何显示网络更改状态的对话?

时间:2019-07-20 10:37:47

标签: java android android-fragments android-activity dialog

我在android中有一个用于网络更改状态的代码。但是现在的问题是,当网络状态更改时,代码显示了小吃店。我希望当网络状态更改时它将显示对话框。我已经做了一切。我只是想代替小吃店对话将被显示。

IntentFilter intentFilter = new IntentFilter(NetworkStateChangedReceiver.NETWORK_AVAILABLE_ACTION);
        LocalBroadcastManager.getInstance(this).registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                boolean isNetworkAvailable = intent.getBooleanExtra(IS_NETWORK_AVAILABLE, false);
                String networkStatus = isNetworkAvailable ? "connected" : "disconnected";

                Snackbar.make(findViewById(R.id.activity_dashboard), "Network Status: " + networkStatus, Snackbar.LENGTH_LONG).show();
            }
        }, intentFilter);

2 个答案:

答案 0 :(得分:0)

只需使用AlertDialog.Builder而不是Snackbar创建警报对话框即可。遵循以下内容应该可以,

IntentFilter intentFilter = new IntentFilter(NetworkStateChangedReceiver.NETWORK_AVAILABLE_ACTION);
LocalBroadcastManager.getInstance(this).registerReceiver(new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        boolean isNetworkAvailable = intent.getBooleanExtra(IS_NETWORK_AVAILABLE, false);
        String networkStatus = isNetworkAvailable ? "connected" : "disconnected";

        //Snackbar.make(findViewById(R.id.activity_dashboard), "Network Status: " + networkStatus, Snackbar.LENGTH_LONG).show();

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.dialog_message).setTitle(R.string.dialog_title);
        AlertDialog dialog = builder.create();
    }
}, intentFilter);

答案 1 :(得分:0)

这是您可以使用的简单AlertDialog:

 public static Dialog createSimpleOkDialog(Context context, String title, String message) {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(context)
                .setTitle(title)
                .setMessage(message)
                .setNegativeButton(android.R.string.ok, null);

        return alertDialog.create();
    }