原文地址:http://blog.csdn.net/geolo/article/details/6011132
一. 监听和判断网络状态(转贴:http://wang-peng1.javaeye.com/blog/557362):
- public class ConnectionChangeReceiver extends BroadcastReceiver {
- @Override
- public void onReceive( Context context, Intent intent ) {
- ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
- NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
- NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE );
- if ( activeNetInfo != null ) {
- Toast.makeText( context, "Active Network Type : " + activeNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
- }
- if( mobNetInfo != null ) {
- Toast.makeText( context, "Mobile Network Type : " + mobNetInfo.getTypeName(), Toast.LENGTH_SHORT ).show();
- }
- }
- }
- <!-- Needed to check when the network connection changes -->
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
- <receiver android:name="com.blackboard.androidtest.receiver.ConnectionChangeReceiver"
- android:label="NetworkConnection">
- <intent-filter>
- <action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
- </intent-filter>
- </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:
- Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)
- Send broadcast intents when network connectivity changes
- Attempt to "fail over" to another network when connectivity to a network is lost
- 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还可以监听网络断开、正在连接和连接成功。
- final TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
- mTelephonyMgr.listen(new PhoneStateListener(){
- @Override
- public void onDataConnectionStateChanged(int state) {
- switch(state){
- case TelephonyManager.DATA_DISCONNECTED://网络断开
- break;
- case TelephonyManager.DATA_CONNECTING://网络正在连接
- break;
- case TelephonyManager.DATA_CONNECTED://网络连接上
- break;
- }
- }
- }
PhoneStateListener.LISTEN_DATA_CONNECTION_STATE); 我们只要重载onDataConnectionStateChanged方法,根据state判断做相应的处理。
三、android 判断是否有可用网络(转:http://yangguangfu.javaeye.com/blog/723334)
- public boolean CheckNetwork() {
- boolean flag = false;
- ConnectivityManager cwjManager = (ConnectivityManager) AndroidBaseActivity.self.getSystemService(Context.CONNECTIVITY_SERVICE);
- if (cwjManager.getActiveNetworkInfo() != null)
- flag = cwjManager.getActiveNetworkInfo().isAvailable();
- if (!flag) {
- Builder b = new AlertDialog.Builder( AndroidBaseActivity.self).setTitle("没有可用的网络").setMessage( AndroidBaseActivity.self.getResources().getString(R.string.net_work_message));
- b.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- Intent mIntent = new Intent("/");
- ComponentName comp = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
- mIntent.setComponent(comp);
- mIntent.setAction("android.intent.action.VIEW");
- AndroidBaseActivity.self.startActivity(mIntent);
- }
- }).setNeutralButton("取消", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- dialog.cancel();
- }
- }).create();
- b.show();
- }
- Toast.makeText(AndroidBaseActivity.self, "test", 1).show();
- return flag;
- }
这部分只有两个地方的代码是主要的:
一个是: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)
- ContentValues values = new ContentValues();
- values.put(NAME, "CMCC cmwap");
- values.put(APN, "cmwap");
- values.put(PROXY, "10.0.0.172");
- values.put(PORT, "80");
- values.put(MMSPROXY, "");
- values.put(MMSPORT, "");
- values.put(USER, "");
- values.put(SERVER, "");
- values.put(PASSWORD, "");
- values.put(MMSC, "");
- values.put(TYPE, "");
- values.put(MCC, "460");
- values.put(MNC, "00");
- values.put(NUMERIC, "46000");
- reURI = getContentResolver().insert(Uri.parse("content://telephony/carriers"), values);
- //首选接入点"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);
}