Android开发(12) 无线网络和GPRS连接

概述

启用gprs连接?难道说不是自动就调用网络了么?是呀,android 会默认调用当前的活动的网络. 那么什么是活动的网络呢?
看如下代码:

  ConnectivityManager cm = (ConnectivityManager) m_context
  .getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();

使用服务方式获得连接管理器,然后获得当前的活动网络信息.那么这个网络信息都可能是什么呢?
我在说使用中发现,如果wifi打开的话,那么当前的活动网络指向wifi.如果未打开,那么指向默认的apn.

什么是APN?

看下来自百度百科的解释:APN(Access Point Name),即“接入点名称”,是您在通过手机上网时必须配置的一个参数,它决定了您的手机通过哪种接入方式来访问网络,用来标识GPRS的业务种类,目前分为两大类:CMWAP/UNIWAP/3GWAP(通过GPRS访问WAP业务)、CMNET/UNINET/3GNET(除了WAP以外的服务目前都用CMNET,比如连接因特网等)。<摘自:http://baike.baidu.com/view/668.htm>

简单来说: APN 就是一个连接的设置.通过apn可以设置一个拨号连接,通过该拨号连接可以连接到 "网络提供商" 的服务器,由网络
提供商为你提供接入互联网的服务.这个网络提供商,可能是中国移动,联通,电信.

那么这个活动网络的优先级别可能是:
1.如果wifi打开,则指向wif
2.指向一个默认的(活动的,或者'当前的')apn.

APN怎么设置?

一般是用户设置好的.如果你是中国移动的手机卡,那你就要设置 中国移动提供的apn地址.这个地址一般都是固定的,网上也都能搜索到.


那么切入正题,我们在开发软件的时候,有时候需要调用网络连接.可能我们已经写好一个程序,比如说.我们写一个发送EMAIL的程序,
该程序会接入互联网,将EMAIL发送出去.那么在我们的这个应用程序中,我们如何写代码调用当前的网络么?其实...

答案是: 什么都不用写?
吓,开玩笑是吧? 玩我的是吧? 怎么能不写?
我们在本文开头大概了解了,活动网络.实际上android或默认 通过 当前的活动的网络 来为应用程序提供网络的使用服务.也就是说你的
应用程序在调用网络时,会默认使用 android系统默认的活动网络连接.那么,我们如果开启了wifi正常,如果没开启wifi,那就是用默认的apn.实际上,你的应用程序可以仅仅做到这一步. 网络连接的方式交给用户自己去连接,使用者决定使用是wifi,或者自己切换apn.android系统提供了移动网络的设置功能。

问题是: 这样真的行么? 我们看看实际使用环境会出现的问题,apn有很多中,可能是 中国移动的wmwap,wmnet,3g,联通的,电信的等. 由于网络运行商不同,提供的apn接入方式也不同..那可咋办?如果我是移动用户,我的apn指向wap方式,而我的应用程序是需要wmnet方式的.那我的程序就不能用了,必须让用户手动切换APN才行,而用户自己可能根本不懂apn(也可能懂,但很麻烦).

我的解决方案:

1.先获得当前活动的网络,尝试连接,如果可用(通畅),那么就用当前的.
2.如果不可能,活动默认的apn,再次尝试连接,如果通畅,则用当前的.
3.遍历apn列表,尝试连接,如果可能,则用.

也就是.尝试多个方式,直到找到一个可以用的为止.方法很笨,但是能解决问题.

APN相关的操作

android提供了contentProvider来提供apn的数据.我们直接操作这个就行了.切换apn时,使用content://telephony/carriers/preferapn,
更改这个内容就行了.
如果遍历apn? 数据存储在 content://telephony/carriers.同样用contentProvider来操作数据.
更多的操作数据的和contentProvider的使用,请阅读开发文档和google

贴出我的代码.我写了两个类,一个操作apn的,一个用于多次尝试连接网络的类.使用多次尝试连接 http://www.baidu.com/ 来确定是否连接到互联网.

  package surveySys.Core;
    
    import java.util.ArrayList;
    import java.util.List;
    import android.content.BroadcastReceiver;
    import android.content.ContentResolver;
    import android.content.ContentValues;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.database.Cursor;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.net.Uri;
    import android.util.Log;
    
    public class APNHelper {
     public static final Uri CURRENT_APN_URI = Uri
       .parse("content://telephony/carriers/preferapn");
    
     public static final Uri APN_URI = Uri.parse("content://telephony/carriers");
    
     private Context m_context;
     private ContentResolver m_ContentResolver;
    
     public APNHelper(Context ctx) {
      super();
      if (ctx == null) {
       throw new NullPointerException("m_context");
      }
      m_context = ctx;
      m_ContentResolver = m_context.getContentResolver();
     }
    
     /*
      * 获得当前的apn编号
      */
     public String getCurrentApnId() {
      Cursor cur = null;
      try {
       cur = m_ContentResolver.query(CURRENT_APN_URI, null, null, null,
         null);
       if (cur != null && cur.moveToFirst()) {
        String apnID = cur.getString(cur.getColumnIndex("_id"));
        cur.close();
        return apnID;
       }
       return null;
      } catch (Exception e) {
       e.printStackTrace();
       return null;
      } finally {
       if (cur != null) {
        cur.close();
       }
      }
    
     }
    
     /*
      * 获得当前的apn
      */
     public APN getCurrentAPN() {
      List<APN> lst = getApnList();
      String id = getCurrentApnId();
      APN tmp = null;
      for (APN apn : lst) {
       if (apn._id.equals(id)) {
        tmp = apn;
        break;
       }
      }
      return tmp;
     }
    
     /*
      * 设置当前的apn
      */
     public boolean setCurrentAPN(String apnId) {
      try {
       ContentValues values = new ContentValues();
       values.put("apn_id", apnId);
       m_ContentResolver.update(CURRENT_APN_URI, values, null, null);
    
       return true;
      } catch (Exception e) {
       e.printStackTrace();
       return false;
      }
     }
    
     /*
      * 获得apn列表
      */
     public List<APN> getApnList() {
      return query(null);
     }
    
     /*
      * 获得有效的apn列表
      */
     public List<APN> getValidApnList() {
      return query("current=1");
     }
    
     public class APN {
      public String name;
      public String numeric;
      public String _id;
      public String mcc;
      public String mnc;
      public String apn;
      public String user;
      public String server;
      public String password;
      public String proxy;
      public String port;
      public String mmsproxy;
      public String mmsport;
      public String mmsc;
      public String authtype;
      public String type;
      public String current;
    
      public APN(String name, String numeric, String id, String mcc,
        String mnc, String apn, String user, String server,
        String password, String proxy, String port, String mmsproxy,
        String mmsport, String mmsc, String authtype, String type,
        String current) {
       super();
       this.name = name;
       this.numeric = numeric;
       _id = id;
       this.mcc = mcc;
       this.mnc = mnc;
       this.apn = apn;
       this.user = user;
       this.server = server;
       this.password = password;
       this.proxy = proxy;
       this.port = port;
       this.mmsproxy = mmsproxy;
       this.mmsport = mmsport;
       this.mmsc = mmsc;
       this.authtype = authtype;
       this.type = type;
       this.current = current;
      }
    
      public APN() {
       super();
      }
    
      public String toString() {
    
       return String
         .format(
           "name=%s,numeric=%s,_id=%s,mcc=%s,mnc=%s,apn=%s,user=%s,server=%s,password=%s,proxy=%s,port=%s,mmsproxy=%s,mmsport=%s,mmsc=%s, authtype=%s,type=%s,current=%s,",
           name, this.numeric, _id, mcc, mnc, apn, user,
           server, password, proxy, port, mmsproxy, mmsport,
           mmsc, authtype, type, current);
      }
     }
    
     /*
      * 查找
      */
     public List<APN> query(String query) {
      List<APN> lst = new ArrayList<APN>();
      Cursor cur = null;
      try {
       cur = m_ContentResolver.query(APN_URI, null, query, null, null);
       while (cur != null && cur.moveToNext()) {
        APN apn = new APN();
        apn.name = cur.getString(cur.getColumnIndex("name"));
        apn.numeric = cur.getString(cur.getColumnIndex("numeric"));
        apn._id = cur.getString(cur.getColumnIndex("_id"));
        apn.mcc = cur.getString(cur.getColumnIndex("mcc"));
        apn.mnc = cur.getString(cur.getColumnIndex("mnc"));
        apn.apn = cur.getString(cur.getColumnIndex("apn"));
        apn.user = cur.getString(cur.getColumnIndex("user"));
        apn.server = cur.getString(cur.getColumnIndex("server"));
        apn.password = cur.getString(cur.getColumnIndex("password"));
        apn.proxy = cur.getString(cur.getColumnIndex("proxy"));
        apn.port = cur.getString(cur.getColumnIndex("port"));
    
        apn.mmsproxy = cur.getString(cur.getColumnIndex("mmsproxy"));
        apn.mmsport = cur.getString(cur.getColumnIndex("mmsport"));
        apn.mmsc = cur.getString(cur.getColumnIndex("mmsc"));
        apn.authtype = cur.getString(cur.getColumnIndex("authtype"));
        apn.type = cur.getString(cur.getColumnIndex("type"));
        apn.current = cur.getString(cur.getColumnIndex("current"));
        lst.add(apn);
       }
       return lst;
      } catch (Exception e) {
       e.printStackTrace();
       return null;
      } finally {
       if (cur != null) {
        cur.close();
       }
      }
     }
    
    }

代码:

    package surveySys.Core;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.List;
    
    import surveySys.Core.APNHelper.APN;
    import surveySys.Dev.TestModule.ActTest;
    
    import android.content.Context;
    import android.content.Intent;
    import android.net.ConnectivityManager;
    import android.net.NetworkInfo;
    import android.net.NetworkInfo.State;
    import android.provider.Settings;
    
    public class NetHelper {
    
     private Context m_context;
     // private static final String URLNameForTry = "http://www.google.com/";
    
     public String URL_ForTry = "http://www.baidu.com/";
    
     public NetHelper(Context context) {
      super();
      if (context == null) {
       throw new NullPointerException("context");
      }
      m_context = context;
     }
    
     private boolean open(String urlForTry) {
      
      this.URL_ForTry = urlForTry;
      if (checkNetworkState(m_context)) {
       if (tryGetURL(m_context, urlForTry)) {
        return true;
       }
      }
      // 判断网络是否可用,不可用,尝试启用 gprs APN
      if (trySwitchToValidApn(m_context, urlForTry)) {
       return true;
      }
      return false;
     }
    
     public static boolean TryConnect(Context ctx, String urlForTry) {
      if (ctx == null) {
       throw new NullPointerException("Context");
      }
      if(!urlForTry.startsWith("http://"))
      {
       throw new IllegalArgumentException("测试使用的网址参数参数必须以http://开头");
      }
      NetHelper scope = null;
      scope = new NetHelper(ctx);
      return scope.open(urlForTry);
     }
    
     /*
      * 无线网络配置界面
      */
     public static void OpenActivityWIRELESS_SETTINGS(Context ctx) {
      // startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
      // //进入手机中的wifi网络设置界面
      ctx.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));// 进入无线网络配置界面
     }
    
     /*
      * 进入手机中的wifi网络设置界面
      */
     public static void OpenActivityWIFI_SETTINGS(Context ctx) {
      ctx.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
     }
    
     /*
      * 检测网络设置
      */
     private boolean checkNetworkState(Context context) {
    
      ConnectivityManager conMan = (ConnectivityManager) context
        .getSystemService(Context.CONNECTIVITY_SERVICE);
    
      NetworkInfo[] infoArray = conMan.getAllNetworkInfo();
      for (int i = 0; i < infoArray.length; i++) {
       NetworkInfo var = infoArray[i];
       System.out.println(String.format("%s", var));
      }
    
      // mobile 3G Data Network
      State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
        .getState();
    
      State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
        .getState();
    
      if (wifi == State.CONNECTED || wifi == State.CONNECTING) {
       return true;
      }
    
      // 如果3G网络和wifi网络都未连接,且不是处于正在连接状态 则进入Network Setting界面 由用户配置网络连接
      if (mobile == State.CONNECTED || mobile == State.CONNECTING) {
       return true;
      }
    
      return false;
     }
    
     public static String getCurrentNetInfo(Context ctx) {
      ConnectivityManager cm = (ConnectivityManager) ctx
        .getSystemService(Context.CONNECTIVITY_SERVICE);
      NetworkInfo info = cm.getActiveNetworkInfo();
      String str = "无";
      if (info != null) {
    
       str = info.getState().name();
      }
      return String.format("%s,%s,%s", info.getTypeName(), info
        .getExtraInfo(), info.getState());
     }
    
     /*
      * 超时时间10秒
      */
     public boolean tryGetURL(Context ctx, String urlStr) {
      try {
       URL url = new URL(urlStr);
       HttpURLConnection urlConn = (HttpURLConnection) url
         .openConnection();
    
       // 使用HttpURLConnection打开连接
       HttpURLConnection urlConn1 = (HttpURLConnection) url
         .openConnection();
       // 得到读取的内容(流)
    
       InputStreamReader in = new InputStreamReader(urlConn
         .getInputStream());
       // 为输出创建BufferedReader
    
       String resultData = "";
       BufferedReader buffer = new BufferedReader(in);
       String inputLine = null;
       // 使用循环来读取获得的数据
       while (((inputLine = buffer.readLine()) != null)) {
        // 我们在每一行后面加上一个"\n"来换行
        resultData += inputLine + "\n";
       }
       // 关闭InputStreamReader
       in.close();
       // 关闭http连接
       urlConn.disconnect();
    
       return resultData != null;
      } catch (Exception e) {
       e.printStackTrace();
       String str = e.getMessage();
       return false;
      }
     }
    
     private boolean trySwitchToValidApn(Context context, String urlForTry) {
      try {
       APNHelper helper = new APNHelper(context);
       APN currentApn = helper.getCurrentAPN();
       boolean isConnectied = false;
       if (currentApn != null) {
        isConnectied = tryGetURL(context, urlForTry);
        if (isConnectied) {
         return true;
        }
       }
       List<APN> lst = helper.getValidApnList();
       for (APN apnTemp : lst) {
        if (apnTemp == null) {
         continue;
        }
        if (currentApn != null) {
         if (apnTemp._id.equals(currentApn._id)) {
          continue;
         }
        }
        helper.setCurrentAPN(apnTemp._id);
        try {
         Thread.sleep(11000);
        } catch (Exception e) {
        }
    
        ConnectivityManager cm = (ConnectivityManager) m_context
          .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        String str = "无";
        if (info != null) {
         int i = 0;
         while (info.getState() != State.CONNECTED) {
          try {
           Thread.sleep(1000);
          } catch (Exception e) {
          }
          if (i++ > 3) {
           break;
          }
         }
        }
        isConnectied = tryGetURL(context, urlForTry);
        String st1 = String.format("apn=%s,type=%s,canConnected=%s",
          apnTemp.apn, apnTemp.type, isConnectied);
        System.out.println(st1);
        if (isConnectied) {
         return true;
        }
       }
       return false;
      } catch (Exception e) {
       e.printStackTrace();
       return false;
      }
     }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值