[Android Traffic] Android网络开启、关闭整理
转载: 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网络开启、关闭整理的更多相关文章
- [Android Traffic] 根据网络类型更改下载模式
转载自: http://blog.csdn.net/kesenhoo/article/details/7396321 Modifying your Download Patterns Based on ...
- [Android Traffic] android 流量计算方法
android流量简介 流量统计文件:路径/proc/net/dev 打开文件,其中 lo 为本地流量, rmnet0 为3g/2g流量, wlan0 为无线流量. 在/sys/class/net/下 ...
- Android IntentService 与Alarm开启任务关闭任务
1:MyService public class MyService extends IntentService{ AlarmManager alarmManager = null; PendingI ...
- Android -- Service的开启关闭与生命周期
Service是Android 系统中的四大组件之一,是在一段不定的时间运行在后台,不和用户交互应用组件. service可以在很多场合的应用中使用,比如播放多媒体的时候用户启动了其他Activity ...
- Android开发——监听Android手机的网络状态
0. 前言 在Android开发中监听手机的网络状态是一个常见的功能,比如在没网的状态下进行提醒并引导用户打开网络设置,或者在非wifi状态下开启无图模式等等.因此本篇将网上的资料进行了整理总结,方便 ...
- Android之三种网络请求解析数据(最佳案例)
AsyncTask解析数据 AsyncTask主要用来更新UI线程,比较耗时的操作可以在AsyncTask中使用. AsyncTask是个抽象类,使用时需要继承这个类,然后调用execute()方法. ...
- nginx android app 慢网络请求超时
最近遇到了android 在慢网络下面请求服务器报 java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by ...
- Android热身:通过网络获取资源并更新UI组件
Android热身:通过网络获取资源并更新UI组件 目标 点击"发送请求"按钮,下载某网页的html源码,并显示在TextView控件上:点击"清空",清除Te ...
- Android系列之网络(二)----HTTP请求头与响应头
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
随机推荐
- hdu 2016 数据的交换输出
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2016 题目大意:把最小的和第一个交换并输出.注意格式哦! #include <stdio.h&g ...
- LCD实验学习笔记(四):系统时钟
一般CPU频率(FCLK)高于内存.网卡等设备频率(HCLK),而串口.USB.I2C等设备频率(PCLK)更低. 系统时钟: 系统时钟源为晶振,初始频率12MHz. 通过设置MPLLCON寄存器的M ...
- v4l2 Camera详细设置【转】
转自:http://blog.csdn.net/smilefyx/article/details/39555289 转载自:http://blog.sina.com.cn/s/blog_602f877 ...
- VS mfc MessageBox() 使用英文显示
转载:http://blog.csdn.net/guoyk1990/article/details/44337249 由于特殊原因我们需要将 MessageBox 或 Dialog 的按钮“确定”.“ ...
- Mac-item+zsh
$brew cask install iterm2 $ sh -c "$(curl -fsSL https://raw.github.com/robbyrussell/oh-my-zsh/m ...
- django一对多、多对多模型、自关联的建立
# 原创,转载请留言联系 一对多模型 一对多的关系,例如员工跟部门.一个部门有多个员工.那么在django怎么建立这种表关系呢? 其实就是利用外键,在多的一方,字段指定外键即可.例如员工和部门,员工是 ...
- Selenium2+python自动化57-捕获异常(NoSuchElementException)【转载】
前言 在定位元素的时候,经常会遇到各种异常,为什么会发生这些异常,遇到异常又该如何处理呢? 本篇通过学习selenium的exceptions模块,了解异常发生的原因. 一.发生异常 1.打开博客首页 ...
- 在Debian 9上安装和配置Observium网络监控
https://blog.csdn.net/csgd2000/article/details/80780697
- 使用jsonp进行跨域请求
使用jsonp进行跨域请求 在实际的业务中很多时候需要用到跨域请求,然而jsonp为我们提供了一种非常方便的跨域请求的方式,具体实现代码如下: $.ajax({ type:"get" ...
- 在web项目下注册MySQL数据库驱动失败
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver at org.apache.catalina.loader.WebappClassLoa ...