Android网络开启、关闭整理

news/2024/7/5 19:34:07

Android网络开启、关闭整理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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); 
     }
}

  WiFi、GPRS、飞行模式的开启、关闭以及一些状态的检测


      转载地址:http://www.cnblogs.com/huazaizai/p/3398675.html




http://www.niftyadmin.cn/n/3648606.html

相关文章

.nsh 怎么样运行_如何在Ubuntu 18.04上使用nsh运行安全的远程命令

.nsh 怎么样运行介绍 (Introduction) It can often be difficult to manage multiple machines on a daily basis. While Secure Shell (SSH) is a good choice for remote access, the protocol itself has some drawbacks in both convenience and security. 每天通常很难管理…

WINAMP SDK 常见问题回答(FAQ)

WINAMP SDK 常见问题回答(FAQ) 这里有一些收集的问题&#xff0c;我们不想每个人重蹈覆辙&#xff0c;希望这里变成知识仓库,能够帮助大家。1、我不知道怎样用sdk&#xff0c;你可以帮助我写一个插件吗&#xff1f;一个好的方法就是去下载sdks&#xff0c;然后去察看或修改代…

EventBus的手写

在上一篇文章我讲了EventBus源码&#xff0c;现在我们来手写一下EventBus 使用 MainActiviity public class MainActivity extends AppCompatActivity {private TextView mTv;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceSta…

node.js 创建服务器_如何使用HTTP模块在Node.js中创建Web服务器

node.js 创建服务器The author selected the COVID-19 Relief Fund to receive a donation as part of the Write for DOnations program. 作者选择了COVID-19救济基金来接受捐赠&#xff0c;这是Write for DOnations计划的一部分。 介绍 (Introduction) When you view a webp…

WINAMP 配置文件

WINAMP 配置文件很简单&#xff0c;就没有翻译了 &#xff1b;-&#xff09;Configuration Files Any skin file is not complete without first finishing up the configuration files. These configuration files control such things as the background and foreground colo…

Android AES加密算法及其实现

昨天老大叫我看看android加密算法。于是网上找了找&#xff0c;找到了AES加密算法。&#xff08;当然还有MD5,BASE64什么的http://snowolf.iteye.com/blog/379860这篇文章列举了很多&#xff0c;但是基本都是j2se平台的&#xff0c;android平台不一定支持&#xff0c;但是AES算…

系统分析员论文样例

系统分析员论文样例中石化金卡工程江苏省联合办公室 尤一浩 &#xff08;江苏省南京市中山南路242号 邮编&#xff1a;210005 电话&#xff1a;4209423&#xff09;江苏省石油集团公司信息技术管理处 司文全 &#xff08;江苏省南京市中山南路242号 邮编&#xff1a;210005 电话…

服务器安装ansible_通过Ansible自动完成服务器设置的复杂性

服务器安装ansible视频 (Video) Server automation now plays an essential role in systems administration, due to the disposable nature of modern application environments. Configuration management tools such as Ansible are typically used to streamline the proc…