为了账号安全,请及时绑定邮箱和手机立即绑定

如何从 BroadcastReceiver 内部打开 Wifi 设置

如何从 BroadcastReceiver 内部打开 Wifi 设置

慕勒3428872 2023-10-12 10:05:40
我正在开发一个 Android 应用程序,它可以识别设备是否连接到网络。如果没有,应用程序会显示AlertDialog并让用户有机会转到设备设置并打开 wifi。我已经为这项工作创建了一个BroadcastReceiver,但我不知道如何创建一个AlertDialog并为用户提供启用 wifi 的选项。这是 的代码BroadcastReceiver。public class ExampleBroadcastReceiver extends BroadcastReceiver {@Overridepublic void onReceive(final Context context, Intent intent) {    if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {        boolean noConnectivity = intent.getBooleanExtra(                ConnectivityManager.EXTRA_NO_CONNECTIVITY, false        );        if (noConnectivity) {            AlertDialog.Builder builder1 = new AlertDialog.Builder(context);            builder1.setMessage("You must have internet connection");            builder1.setCancelable(true);            builder1.setPositiveButton(                    "Yes",                    new DialogInterface.OnClickListener() {                        public void onClick(DialogInterface dialog, int id) {                        }                    });            builder1.setNegativeButton(                    "No",                    new DialogInterface.OnClickListener() {                        public void onClick(DialogInterface dialog, int id) {                            dialog.cancel();                        }                    });            builder1.create();            builder1.show();        } else {            Toast.makeText(context, "Connected", Toast.LENGTH_SHORT).show();        }    }}}
查看完整描述

1 回答

?
温温酱

TA贡献1752条经验 获得超4个赞

请注意,以下代码仅供您参考。您可以根据您的要求更新/更改此代码。


public class MainActivity extends AppCompatActivity {


    private static final String TAG = MainActivity.class.getSimpleName();

    private BroadcastReceiver mNetworkReceiver;




    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        registerNetworkBroadcastForNougat();


    }


private void registerNetworkBroadcastForNougat() {

    mNetworkReceiver = new NetworkChangeReceiver();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

        registerReceiver(mNetworkReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

        registerReceiver(mNetworkReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

    }

}


protected void unregisterNetworkChanges() {

    try {

        unregisterReceiver(mNetworkReceiver);

    } catch (IllegalArgumentException e) {

        e.printStackTrace();

    }

}


@Override

public void onDestroy() {

    super.onDestroy();

    unregisterNetworkChanges();

}




class NetworkChangeReceiver extends BroadcastReceiver {


    @Override

    public void onReceive(Context context, Intent intent)

    {

        try

        {

            if (!isOnline(context)) {

                showDialog(context);

            }


        } catch (NullPointerException e) {

            e.printStackTrace();

        }

    }


    private void showDialog(final Context context) {

        AlertDialog.Builder builder1 = new AlertDialog.Builder(context);

        builder1.setMessage("You must have internet connection");

        builder1.setCancelable(true);


        builder1.setPositiveButton(

                "Yes",

                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int id) {

                        startActivity(new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK));

                    }

                });


        builder1.setNegativeButton(

                "No",

                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int id) {

                        dialog.cancel();

                    }

                });

        builder1.create();

        builder1.show();

    }


    private 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;

        }

    }

}

}


查看完整回答
反对 回复 2023-10-12
  • 1 回答
  • 0 关注
  • 68 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信