转载: http://blog.csdn.net/tu_bingbing/article/details/8469871

近段时间由于要对手机网络状况进行判断、开启和关闭,从网上找了些资料,现整理如下

包含了对WiFi、GPRS、飞行模式的开启、关闭以及一些状态的检测,在小米和三星平板上测试均通过

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

package com.my.device_admin.business;

import java.lang.reflect.Method;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings; public class NetworkManager { private Context context;
private ConnectivityManager connManager; public NetworkManager(Context context) {
this.context = context;
connManager = (ConnectivityManager) this.context
.getSystemService(Context.CONNECTIVITY_SERVICE);
} /**
* @return 网络是否连接可用
*/
public boolean isNetworkConnected() { NetworkInfo networkinfo = connManager.getActiveNetworkInfo(); if (networkinfo != null) {
return networkinfo.isConnected();
} return false;
} /**
* @return wifi是否连接可用
*/
public boolean isWifiConnected() { NetworkInfo mWifi = connManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (mWifi != null) {
return mWifi.isConnected();
} return false;
} /**
* 当wifi不能访问网络时,mobile才会起作用
* @return GPRS是否连接可用
*/
public boolean isMobileConnected() { NetworkInfo mMobile = connManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mMobile != null) {
return mMobile.isConnected();
}
return false;
} /**
* GPRS网络开关 反射ConnectivityManager中hide的方法setMobileDataEnabled 可以开启和关闭GPRS网络
*
* @param isEnable
* @throws Exception
*/
public void toggleGprs(boolean isEnable) throws Exception {
Class<?> cmClass = connManager.getClass();
Class<?>[] argClasses = new Class[1];
argClasses[0] = boolean.class; // 反射ConnectivityManager中hide的方法setMobileDataEnabled,可以开启和关闭GPRS网络
Method method = cmClass.getMethod("setMobileDataEnabled", argClasses);
method.invoke(connManager, isEnable);
} /**
* WIFI网络开关
*
* @param enabled
* @return 设置是否success
*/
public boolean toggleWiFi(boolean enabled) {
WifiManager wm = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
return wm.setWifiEnabled(enabled); } /**
*
* @return 是否处于飞行模式
*/
public boolean isAirplaneModeOn() {
// 返回值是1时表示处于飞行模式
int modeIdx = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);
boolean isEnabled = (modeIdx == 1);
return isEnabled;
}
/**
* 飞行模式开关
* @param setAirPlane
*/
public void toggleAirplaneMode(boolean setAirPlane) {
Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, setAirPlane ? 1 : 0);
// 广播飞行模式信号的改变,让相应的程序可以处理。
// 不发送广播时,在非飞行模式下,Android 2.2.1上测试关闭了Wifi,不关闭正常的通话网络(如GMS/GPRS等)。
// 不发送广播时,在飞行模式下,Android 2.2.1上测试无法关闭飞行模式。
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
// intent.putExtra("Sponsor", "Sodino");
// 2.3及以后,需设置此状态,否则会一直处于与运营商断连的情况
intent.putExtra("state", setAirPlane);
context.sendBroadcast(intent);
}
}

[Android Traffic] Android网络开启、关闭整理的更多相关文章

  1. [Android Traffic] 根据网络类型更改下载模式

    转载自: http://blog.csdn.net/kesenhoo/article/details/7396321 Modifying your Download Patterns Based on ...

  2. [Android Traffic] android 流量计算方法

    android流量简介 流量统计文件:路径/proc/net/dev 打开文件,其中 lo 为本地流量, rmnet0 为3g/2g流量, wlan0 为无线流量. 在/sys/class/net/下 ...

  3. Android IntentService 与Alarm开启任务关闭任务

    1:MyService public class MyService extends IntentService{ AlarmManager alarmManager = null; PendingI ...

  4. Android -- Service的开启关闭与生命周期

    Service是Android 系统中的四大组件之一,是在一段不定的时间运行在后台,不和用户交互应用组件. service可以在很多场合的应用中使用,比如播放多媒体的时候用户启动了其他Activity ...

  5. Android开发——监听Android手机的网络状态

    0. 前言 在Android开发中监听手机的网络状态是一个常见的功能,比如在没网的状态下进行提醒并引导用户打开网络设置,或者在非wifi状态下开启无图模式等等.因此本篇将网上的资料进行了整理总结,方便 ...

  6. Android之三种网络请求解析数据(最佳案例)

    AsyncTask解析数据 AsyncTask主要用来更新UI线程,比较耗时的操作可以在AsyncTask中使用. AsyncTask是个抽象类,使用时需要继承这个类,然后调用execute()方法. ...

  7. nginx android app 慢网络请求超时

    最近遇到了android 在慢网络下面请求服务器报 java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by ...

  8. Android热身:通过网络获取资源并更新UI组件

    Android热身:通过网络获取资源并更新UI组件 目标 点击"发送请求"按钮,下载某网页的html源码,并显示在TextView控件上:点击"清空",清除Te ...

  9. Android系列之网络(二)----HTTP请求头与响应头

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

随机推荐

  1. mysql六:索引原理与慢查询优化

    一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...

  2. 一次 select for update 的悲观锁使用引发的生产事故

    1.事故描述 本月 8 日上午十点多,我们的基础应用发生生产事故.具体表象为系统出现假死无响应.查看事发时间段的基础应用 error 日志,没发现明显异常.查看基础应用业务日志,银行结果处理的部分普遍 ...

  3. k8s通过configmap管理应用配置信息

    Secret 可以为 Pod 提供密码.Token.私钥等敏感数据:对于一些非敏感数据,比如应用的配置信息,则可以用 ConfigMap. ConfigMap 的创建和使用方式与 Secret 非常类 ...

  4. eps图片中有中文乱码的问题

    一般的,如果matlab中的fig图片中有中文,直接saveas为eps,eps再插入latex后会出现乱码. 解决的办法为: (1) *.fig利用‘file--print’保存为*.pdf (2) ...

  5. 成都项目中因为MYSQL与SSDB备分时间不一致,导致主键产生器错误解决一例

    -- JFinal错误提示 Duplicate entry '1791361-1823391' for key 'PRIMARY' -- 1.查看SSDB的主键生成器值ssdb 127.0.0.1:8 ...

  6. 在Servlet中获取Spring注解的bean

    最近由于项目中出现了Servlet调用Spring的bean,由于整个项目中所有的bean均是注解方式完成,如@Service,@Repository,@Resource等,但是Spring的容器管理 ...

  7. PHP serialize 和 JSON 解析与区别

    PHP serialize 和 JSON 解析与区别 对于JSON(JavaScript Object Notation)大家应该不陌生,它是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器 ...

  8. [orangehrm] 安装问题集合

    Web server allows .htaccess files # 这一项检查不通过 解决: In conf/extra/httpd-vhosts.conf, add the line Allow ...

  9. Codeforces 863F - Almost Permutation

    863F - Almost Permutation 题意 给出每个位置可以放的数字的范围,定义 \(cost = \sum_{i=1}^{n}(cnt(i))^2\) ,其中 \(cnt(i)\) 为 ...

  10. window下安装rsyncServer

    window下安装rsyncServer---------------------------------1. 解压cwRsyncServer_4.0.5_Installer.zip,安装. 2. 复 ...