码迷,mamicode.com
首页 > 移动开发 > 详细

app检查网络

时间:2015-07-22 16:40:31      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

 

权限:

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

 

//用于判断和开启网络设置
public class NetCheckWebServer {
 public NetCheckWebServer() {
 }

 public static boolean networkStatus(Context context) {
  boolean flag;
  NetworkInfo networkinfo;
  NetworkInfo networkinfo1;
  flag = true;
  ConnectivityManager connectivitymanager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  networkinfo = connectivitymanager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
  networkinfo1 = connectivitymanager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
  if ((networkinfo == null || !networkinfo.isConnectedOrConnecting())&& (networkinfo1 == null || !networkinfo1.isConnectedOrConnecting())) {
   openNetworkSettings(context);
   flag = false;
  }
  return flag;
 }
    //跳转到设置界面,手动修改网络状态
 private static void openNetworkSettings(final Context context) {
  (new android.app.AlertDialog.Builder(context))
    .setTitle("未发现网络服务")
    .setMessage("是否开启网络服务")
    .setPositiveButton("确定",
      new android.content.DialogInterface.OnClickListener() {

       public void onClick(
         DialogInterface dialoginterface, int i) {

        // 手机系统版本 大于10 就是3.0或以上版本
        if (Integer
          .parseInt(android.os.Build.VERSION.SDK) > 10) {
         Intent intent = new Intent(
           "android.settings.WIRELESS_SETTINGS");
         context.startActivity(intent);
        } else {
         Intent intent1 = new Intent("/");
         intent1.setComponent(new ComponentName(
           "com.android.settings",
           "com.android.settings.WirelessSettings"));
         intent1.setAction("android.intent.action.VIEW");
         context.startActivity(intent1);
         dialoginterface.cancel();
        }
       }
      })
    .setNegativeButton("取消",
      new android.content.DialogInterface.OnClickListener() {
       public void onClick(
         DialogInterface dialoginterface, int i) {
        dialoginterface.dismiss();
       }
      }).show();
 }
}

app检查网络

标签:

原文地址:http://my.oschina.net/u/2406195/blog/482373

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!