Android中打开网络设置界面
16lz
2021-01-26
1,我们判断网络是否打开:
public static boolean isConnectNet(Context context){ boolean bisConnFlag=false; ConnectivityManager conManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo network = conManager.getActiveNetworkInfo(); if(network!=null){ bisConnFlag=conManager.getActiveNetworkInfo().isAvailable(); } return bisConnFlag; }
2. 未开启网络时打开设置界面(如果不写在Activity里面则不需要参数),在相应的位置调用即可
private voidsetNetwork(Context context){
Intent intent=null; //判断手机系统的版本 即API大于10 就是3.0或以上版本 if(android.os.Build.VERSION.SDK_INT>10){ intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS); }else{ intent = new Intent(); ComponentName component = new ComponentName("com.android.settings","com.android.settings.WirelessSettings"); intent.setComponent(component); intent.setAction("android.intent.action.VIEW"); } context.startActivity(intent); }
在 AndroidManifest.xml中设置权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.INTERNET"/>
更多相关文章
- Android(安卓)studio如何使用SVN进行版本控制?
- Android学习笔记(三):Andriod程序框架
- Gradle for Android(安卓)第四篇( 构建变体 )
- 使用IntelliJ IDEA(androidstudio前身)开发android移动应用前的基
- Android命令行工具AAPT2
- Android培训---创建Android工程
- 【OpenCV】编译opencv+opencv_contrib sdk for android(附编译好
- 关于android设备唯一区分device id的取得
- 【记录】Android(安卓)广播的发送与接收