💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
***** **网络判断** [TOC=6] # 1. 网络连接的判断(重点) 注意:必须添加网络状态的权限 ~~~ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> ~~~ 判断网络 ~~~ /** * 判断网络是否可用 */ public boolean isNetworkConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (mNetworkInfo != null) { return mNetworkInfo.isAvailable(); } } return false; } ~~~ # 2. 判断网络类型 ` ~~~ /** * 判断网络类型 */ public static int getConnectedType(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (mNetworkInfo != null && mNetworkInfo.isAvailable()) { return mNetworkInfo.getType(); } } return -1; } ~~~ ` # 3. 没有网络,提示用户(跳转) 全部网络设置(ACTION_WIRELESS_SETTINGS);  WIFI设置(ACTION_WIFI_SETTINGS);  3G流量设置: (ACTION_DATA_ROAMING_SETTINGS). ` ~~~ /** * 当判断当前手机没有网络时选择是否打开网络设置 * @param context */ public static void showNoNetWorkDlg(final Context context) { AlertDialog.Builder builder = new Builder(context); builder.setIcon(R.drawable.ic_launcher) .setTitle(R.string.app_name) .setMessage("当前无网络").setPositiveButton("设置", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 跳转到系统的网络设置界面 Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS); context.startActivity(intent); } }).setNegativeButton("知道了", null).show(); } ~~~ `