1

我为我的 android 应用程序构建了对话框。它运行良好,但我遇到了一些与对话框相关的问题。

(1) 我想要连接互联网或 WiFi 时。自动对话框消失。

(2) 如果互联网连接丢失,应用程序正在运行。对话框再次自动出现。

if (!isConnected(Dashboard.this)) buildDialog(Dashboard.this).show();
        else {
            setContentView(R.layout.activity_dashboard);
        }
 public boolean isConnected(Context context) {

        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netinfo = cm.getActiveNetworkInfo();

        if (netinfo != null && netinfo.isConnectedOrConnecting()) {
            android.net.NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            android.net.NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

            if ((mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null && wifi.isConnectedOrConnecting()))
                return true;
            else return false;
        } else
            return false;
    }

    public AlertDialog.Builder buildDialog(Context c) {

        AlertDialog.Builder builder = new AlertDialog.Builder(c);
        builder.setTitle("No Internet Connection");
        builder.setMessage("You need to have Mobile Data or WiFi to access this. Press OK to Exit");
        builder.setCancelable(false);

        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                Dashboard.super.onBackPressed();
            }
        });

        return builder;
    } 
4

6 回答 6

1

将此行放在警报对话框代码之外

alertDialog.setCancelable(false);
于 2019-06-05T05:38:03.293 回答
1

builder.setCancelable(false);在你的 aleart dailog 中使用属性

于 2019-06-05T05:37:34.700 回答
1

要自动获取 Internet 连接事件,请尝试设置网络更改侦听器。这是一个示例:

/**
 * Broadcast receiver that detects receives notification when change in internet connection to alert when there is no Internet.
 */

public class NetworkChangeReceiver extends BroadcastReceiver {

    private NetworkChangeListener mListener;

    public NetworkChangeReceiver(NetworkChangeListener listener) {
        mListener = listener;
    }

    @Override
    public void onReceive(final Context context, @NonNull final Intent intent) {

        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();

            boolean connected = activeNetwork != null && activeNetwork.isConnected();
            mListener.onNetworkConnectedStateChanged(connected);
        }
    }

    public interface NetworkChangeListener {
        void onNetworkConnectedStateChanged(boolean connected);
    }
}

然后在 Activity 或 Fragment 中注册监听器

@Override
    public void onStart() {
        super.onStart();

        if (mNetworkChangeReceiver == null) {
            mNetworkChangeReceiver = new NetworkChangeReceiver(this);
            getContext().registerReceiver(mNetworkChangeReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
        }
    }

然后显示/关闭关于网络连接状态更改的对话框

@Override
    public void onNetworkConnectedStateChanged(boolean connected) {
        if (connected) {
            //dismiss dialog
        } else {
            //show dialog
        }
    }
于 2019-06-05T07:10:54.003 回答
0

您需要添加广播接收器以获取连接状态。

然后你需要保持 builder 全局。

private AlertDialog.Builder builder;
于 2019-06-05T06:00:08.087 回答
0

您必须 setCancelable == false 才能在对话框之外停止触摸。

在您的情况下,您必须在 setMessage 之后将这样放在AlertDialog.Builder中

builder.setCancelable(false);

并将此功能用于在 Backpress 按钮上的用户您必须 setCancelable == true

为此,您必须像这样在 onBackPressed 中编写代码:-

builder.setCancelable(true);

于 2019-06-05T05:49:10.253 回答
0

只需创建一个BroadcastReceiver来跟踪您的互联网连接

首先,像这样创建一个NetworkChangeReceiver

 public class NetworkChangeReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            try {
                if (isOnline(context)) {
                   //show dialog when internet on
                    dialogHome(true);

                    Log.e("pradeep", "Online Connect Internet ");
                } else {
                  //hide dialog when internet off
                    dialogHome(false);

                    Log.e("pradeep", "Connectivity Failure !!");
                }
            } catch (NullPointerException e) {
                e.printStackTrace();
                Log.i(getClass().getName(), "exceptional " + e.toString());
            }
        }

        public boolean isOnline(Context context) {
            try {
                ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo netInfo = cm.getActiveNetworkInfo();
                //should check null because in airplane mode it will be null
                return (netInfo != null && netInfo.isConnected());
            } catch (NullPointerException e) {
                e.printStackTrace();
                return false;
            }
        }
    }

之后只需在位于AndroidMenifest.xml的应用程序标记中添加以下代码

   <receiver android:name=".utils.NetworkChangeReceiver">
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>

最后一步

只需在您检查互联网活动的活动中创建您的 BroadcastReceiver 实例

    private BroadcastReceiver mNetworkReceiver;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_home);

         //init broadcast receiver
            mNetworkReceiver = new NetworkChangeReceiver();
        }

  @Override
    protected void onResume() {
        super.onResume();
        if (((NetworkChangeReceiver) mNetworkReceiver).isOnline(mContext)) {
        } else {
            registerNetworkBroadcastForNougat();
        }
    }

 private void registerNetworkBroadcastForNougat() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            try {
                registerReceiver(mNetworkReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
            } catch (Exception e) {
                Log.i(getClass().getName(), "easdfdf" + e.toString());
            }
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            try {
                registerReceiver(mNetworkReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
            } catch (Exception e) {
                Log.i(getClass().getName(), "easdfdfdd" + e.toString());
            }
        }
    }

    protected void unregisterNetworkChanges() {
        try {
            unregisterReceiver(mNetworkReceiver);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterNetworkChanges();
    }

每当互联网连接发生变化时 BroadcastReceiver 调用 onReceive 方法。

希望对你有帮助...

于 2019-06-05T08:11:56.893 回答