Android网络判断是否连接和网络类型
来源:互联网 发布:初中生在哪学编程 编辑:程序博客网 时间:2024/06/18 18:50
import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.net.ConnectivityManager;import android.net.NetworkInfo;import android.telephony.TelephonyManager;import static android.provider.Settings.ACTION_WIRELESS_SETTINGS;/** * Created by dell on 2017/11/5. */public class IsConnection { /** * 判断网络是否连接 */ public static boolean getIsConnection(Context context) { ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = manager.getActiveNetworkInfo(); if (info != null && info.isAvailable()) { return true; } else { return false; } } /** * 判断网络类型的方法 */ public static String getNetWorkType(final Context context){ /** *result:只能返回一种结果,网络类型在同一时刻只能有一个类型 */ String result=""; ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = manager.getActiveNetworkInfo(); if (info != null && info.isConnected()){ if (info.getType() == manager.TYPE_WIFI){ /** *返回WIFI网络 */ result = "WIFI"; }else if(info.getType() == manager.TYPE_MOBILE){ /** *返回的是移动流量 */ String name = info.getSubtypeName(); /** * 使用的是移动流量判断是那种类型 */ int type = info.getSubtype(); switch (type){ case TelephonyManager.NETWORK_TYPE_LTE: result = "4G"; break; case TelephonyManager.NETWORK_TYPE_GPRS: case TelephonyManager.NETWORK_TYPE_EDGE: case TelephonyManager.NETWORK_TYPE_CDMA: case TelephonyManager.NETWORK_TYPE_1xRTT: case TelephonyManager.NETWORK_TYPE_IDEN: result = "2G"; break; case TelephonyManager.NETWORK_TYPE_UMTS: case TelephonyManager.NETWORK_TYPE_EVDO_0: case TelephonyManager.NETWORK_TYPE_EVDO_A: case TelephonyManager.NETWORK_TYPE_HSDPA: case TelephonyManager.NETWORK_TYPE_HSUPA: case TelephonyManager.NETWORK_TYPE_HSPA: case TelephonyManager.NETWORK_TYPE_EVDO_B: //api<9 : replace by 14 case TelephonyManager.NETWORK_TYPE_EHRPD: //api<11 : replace by 12 case TelephonyManager.NETWORK_TYPE_HSPAP: result ="3G"; break; default: if (name.equalsIgnoreCase("TD-SCDMA") || name.equalsIgnoreCase("WCDMA") || name.equalsIgnoreCase("CDMA2000")){ result = "3G"; }else{ result = name; } break; } } }else{ /** *如果没有联网就要让用户决定是否去打开网络 */ AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setTitle("设置网络"); builder.setMessage("是否去打开网络?"); builder.setPositiveButton("YES", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { /** * 全部网络设置(ACTION_WIRELESS_SETTINGS); * WIFI设置(ACTION_WIFI_SETTINGS); * 3G流量设置: (ACTION_DATA_ROAMING_SETTINGS). */ Intent intent = new Intent(ACTION_WIRELESS_SETTINGS); context.startActivity(intent); } }); builder.setNegativeButton("NO",null); builder.create(); builder.show(); } return result; }}
阅读全文
0 0
- Android网络判断是否连接和网络类型
- android 判断是否有网络连接,判断网络连接类型
- Android-判断是否有网络连接&&类型
- 判断android网络连接类型与是否连接
- Android判断网络是否连接以及连接类型
- android判断网络是否连接
- android判断是否连接网络
- android判断是否连接网络
- android判断是否连接网络
- android判断网络是否连接
- Android判断网络是否连接
- Android 判断是否连接网络
- android 判断网络是否连接
- android判断网络是否连接
- Android 判断网络是否连接
- android 判断网络是否连接
- Android判断网络是否连接
- Android判断网络是否连接
- 如何输入HTML格式的JMeter测试报告
- ORM系列之一:Dos.ORM
- 浅谈字符串
- (五)BaseNaviController
- 尝试麒麟开源堡垒机遇到的几个小问题
- Android网络判断是否连接和网络类型
- 11.8烦乱一天
- Android 录音 输出格式 mp4编码aac 后缀改成mp3 在IOS端也能播放
- 在vue组件中使用axios
- 数据结构|二叉树的链式存储(实验6.2)
- C++11 std::chrono库详解
- 基于AM335X与FPGA的SPI通讯设计
- Codevs 2822 爱在心中
- js图片上传,判断图片大小