Android中判断网络是否连接并提示设置
/** * 判断网络是否连通 * @param context * @return */ public static boolean isNetworkConnected(Context context){ ConnectivityManager cm = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE); NetworkInfo info = cm.getActiveNetworkInfo(); return info != null && info.isConnected(); }
弹出对话框并设置
//如果网络连接 if(Util.isNetworkConnected(context)){ mMainFrameTask = new MainFrameTask(); mMainFrameTask.execute(); }else{ AlertDialog.Builder builder = new Builder(context); builder.setTitle("设置网络"); builder.setMessage("网络错误,请设置网络"); builder.setPositiveButton("设置网络",new DialogInte易做图ce.OnClickListener() { @Override public void onClick(DialogInte易做图ce dialog, int which) { //获取系统版本号 /* Build.VERSION_CODES 1 (0x00000001) Android 1.0 BASE 2 (0x00000002) Android 1.1 BASE_1_1 3 (0x00000003) Android 1.5 CUPCAKE 4 (0x00000004) Android 1.6 DONUT 5 (0x00000005) Android 2.0 ECLAIR 6 (0x00000006) Android 2.0.1 ECLAIR_0_1 7 (0x00000007) Android 2.1 ECLAIR_MR1 8 (0x00000008) Android 2.2 FROYO 9 (0x00000009) Android 2.3 GINGERBREAD 10 (0x0000000a) Android 2.3.3 GINGERBREAD_MR1 11 (0x0000000b) Android 3.0 HONEYCOMB 12 (0x0000000c) Android 3.1 HONEYCOMB_MR1 13 (0x0000000d) Android 3.2 HONEYCOMB_MR2 */ int currentapiVersion=android.os.Build.VERSION.SDK_INT; System.out.println("currentapiVersion = " + currentapiVersion); Intent intent; if(currentapiVersion < 11){ intent = new Intent(); intent.setClassName("com.android.settings", "com.android.settings.WirelessSettings"); }else{ //3.0以后 //intent = new Intent( android.provider.Settings.ACTION_WIRELESS_SETTINGS); intent = new Intent( android.provider.Settings.ACTION_SETTINGS); } context.startActivity(intent); } }); builder.setNegativeButton("取消", new DialogInte易做图ce.OnClickListener() { @Override public void onClick(DialogInte易做图ce dialog, int which) { } }); builder.create().show(); } }
注意:3.0以前和3.0以后的设置网络方法不同。
补充:移动开发 , Android ,