android 网络管理

原文地址:http://blog.csdn.net/geolo/article/details/6011132

一. 监听和判断网络状态(转贴:http://wang-peng1.javaeye.com/blog/557362):

[java]  view plain copy print ?
  1. public class ConnectionChangeReceiver extends BroadcastReceiver {   
  2.     @Override   
  3.     public void onReceive( Context context, Intent intent ) {   
  4.         ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );   
  5.         NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();   
  6.         NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE );   
  7.         if ( activeNetInfo != null ) {   
  8.             Toast.makeText( context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();   
  9.         }   
  10.         if( mobNetInfo != null ) {   
  11.             Toast.makeText( context, "Mobile Network Type : " + mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();   
  12.         }   
  13.     }   
  14. }   
 

[xhtml]  view plain copy print ?
  1. <!-- Needed to check when the network connection changes -->   
  2. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>   
  3. <receiver android:name="com.blackboard.androidtest.receiver.ConnectionChangeReceiver"   
  4.           android:label="NetworkConnection">   
  5.   <intent-filter>   
  6.     <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>   
  7.   </intent-filter>   
  8. </receiver>   
 

 

 1-1: 注释: ConnectivityManager类的官方解释:

   Class that answers queries about the state of network connectivity. It also notifies applications when network connectivity changes. Get an instance of this class by callingContext.getSystemService(Context.CONNECTIVITY_SERVICE).

The primary responsibilities of this class are to:

  1. Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)
  2. Send broadcast intents when network connectivity changes
  3. Attempt to "fail over" to another network when connectivity to a network is lost
  4. Provide an API that allows applications to query the coarse-grained or fine-grained state of the available networks

 1-2:注释:NetworkInfo类的官方解释:

        1-2-1 Describes the status of a network interface of a given type (currently either Mobile or Wifi).

      1-2-2  NetworkInfo.getTypeName()方法的解释:Return a human-readable name describe the type of the network, for example "WIFI" or "MOBILE".

 

 1-3 :注意添加权限:android.permission.ACCESS_NETWORK_STATE

 

二. Android系列之如何判断网络链接状态(转:http://tech.ddvip.com/2010-07/1280393591158260.html

 

    下面咱们讨论下Android手机判断网络链接状态的技术实现吧。

    目前Android操作系统的手机大部分支持WIFI,GSM,3G网络通信,但是每次链接到网络时只能选择一种链接方式,比如运营商定制的,还必须要求特定的网络环境(CMWAP,CTWAP等)如果要切换网络还需要先关闭现有的网络,然后再启动新的网络,这个转换过程还需要一定的时间,可能程序这时还需要知道心的网络是否链接成功后自动登录到新的网络服务器中,那怎么知道几时链接成功呢?这个时间需要多久呢?也许用一个线程去监听网络状态是否链接成功;我们可以用另外一种方法,PhoneStateListener没错,你没看错,就是用PhoneStateListener。很多应用PhoneStateListener都是监听来电去电,PhoneStateListener还可以监听网络断开、正在连接和连接成功。

  

[java]  view plain copy print ?
  1. final TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);   
  2. mTelephonyMgr.listen(new PhoneStateListener(){  
  3.             
  4.   @Override  
  5.   public void onDataConnectionStateChanged(int state) {  
  6.     switch(state){  
  7.       case TelephonyManager.DATA_DISCONNECTED://网络断开  
  8.         break;  
  9.    case TelephonyManager.DATA_CONNECTING://网络正在连接  
  10.        break;  
  11.     case TelephonyManager.DATA_CONNECTED://网络连接上  
  12.        break;  
  13.   }  
  14. }  
  15.              
  16.  }  
 

 

   PhoneStateListener.LISTEN_DATA_CONNECTION_STATE); 我们只要重载onDataConnectionStateChanged方法,根据state判断做相应的处理。

 

 

三、android 判断是否有可用网络(转:http://yangguangfu.javaeye.com/blog/723334

[java]  view plain copy print ?
  1. public boolean CheckNetwork() {  
  2.           
  3.         boolean flag = false;  
  4.         ConnectivityManager cwjManager = (ConnectivityManager) AndroidBaseActivity.self.getSystemService(Context.CONNECTIVITY_SERVICE);  
  5.         if (cwjManager.getActiveNetworkInfo() != null)  
  6.             flag = cwjManager.getActiveNetworkInfo().isAvailable();  
  7.         if (!flag) {  
  8.             Builder b = new AlertDialog.Builder( AndroidBaseActivity.self).setTitle("没有可用的网络").setMessage( AndroidBaseActivity.self.getResources().getString(R.string.net_work_message));  
  9.             b.setPositiveButton("确定"new DialogInterface.OnClickListener() {  
  10.                 public void onClick(DialogInterface dialog, int whichButton) {  
  11.                     Intent mIntent = new Intent("/");  
  12.                     ComponentName comp = new ComponentName("com.android.settings""com.android.settings.WirelessSettings");  
  13.                     mIntent.setComponent(comp);  
  14.                     mIntent.setAction("android.intent.action.VIEW");  
  15.                     AndroidBaseActivity.self.startActivity(mIntent);  
  16.                 }  
  17.             }).setNeutralButton("取消"new DialogInterface.OnClickListener() {  
  18.                 public void onClick(DialogInterface dialog, int whichButton) {  
  19.                     dialog.cancel();  
  20.                 }  
  21.             }).create();  
  22.             b.show();  
  23.         }  
  24.         Toast.makeText(AndroidBaseActivity.self, "test"1).show();  
  25.        return flag;  
  26.     }  
 

   这部分只有两个地方的代码是主要的:

  一个是:ConnectivityManager cwjManager = (ConnectivityManager) AndroidBaseActivity.self.getSystemService

(Context.CONNECTIVITY_SERVICE); 

  另一个是:cwjManager.getActiveNetworkInfo().isAvailable();  

  这两段代码和第一部分的内容是一样的,只是使用了isAvailable()来检测网络是否处于激活状态。然而对于WIFI这种需要连接到服务器的配置来说这种判断是否有可用的网络是个很需要值得思考的问题了。

   我对这三部分内容的最后了解就是android的ConnectivityManager对所有的网络进行了统一的管理(有点白痴的理解。。。。。)。

四、Android 编程设置 APN (原文:http://www.oschina.net/code/snippet_12_212

[java]  view plain copy print ?
  1. ContentValues values = new ContentValues();  
  2. values.put(NAME, "CMCC cmwap");  
  3. values.put(APN, "cmwap");  
  4. values.put(PROXY, "10.0.0.172");  
  5. values.put(PORT, "80");  
  6. values.put(MMSPROXY, "");  
  7. values.put(MMSPORT, "");  
  8. values.put(USER, "");  
  9. values.put(SERVER, "");  
  10. values.put(PASSWORD, "");  
  11. values.put(MMSC, "");           
  12. values.put(TYPE, "");  
  13. values.put(MCC, "460");  
  14. values.put(MNC, "00");  
  15. values.put(NUMERIC, "46000");  
  16. reURI = getContentResolver().insert(Uri.parse("content://telephony/carriers"), values);  
  17. //首选接入点"content://telephony/carriers/preferapn"  
 

五、开启和关闭wifi

android.permission.ACCESS_WIFI_STATE 

android.permission.CHANGE_WIFI_STATE 
android.permission.WAKE_LOCK

2、获取WifiManager
wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); 

3、开启、关闭wifi
if (wifiManager.isWifiEnabled()) {  
wifiManager.setWifiEnabled(false);  
} else {  
wifiManager.setWifiEnabled(true);  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值