标签:android c style class blog code
**
-
-
-
-
-
-
-
-
- public void testConnectivityManager() {
- ConnectivityManager connManager = (ConnectivityManager) this
- .getSystemService(CONNECTIVITY_SERVICE);
-
- NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
-
- boolean available = networkInfo.isAvailable();
- if(available){
- Log.i("通知", "当前的网络连接可用");
- }
- else{
- Log.i("通知", "当前的网络连接可用");
- }
-
- State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
- if(State.CONNECTED==state){
- Log.i("通知", "GPRS网络已连接");
- }
-
- state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
- if(State.CONNECTED==state){
- Log.i("通知", "WIFI网络已连接");
- }
-
-
- startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
-
- startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
-
- }
(注意:上面的代码只是示例,直接运行的话有待修改。)
上面判断网络类型的方法只是为了展示用法,其实完全可以通过networkInfo.getType()的返回值来判断。
如:
- Context context = listActivity.getApplicationContext();
- ConnectivityManager connectivityManager = (ConnectivityManager) context
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
- if(activeNetInfo.getType()==ConnectivityManager.TYPE_WIFI){
-
- }else if(activeNetInfo.getType()==ConnectivityManager.TYPE_MOBILE) {
-
- }
Android ConnectivityManager,布布扣,bubuko.com
Android ConnectivityManager
标签:android c style class blog code
原文地址:http://www.cnblogs.com/sias/p/3763670.html