Android中使用代码开关GPS、移动网络(GPRS)、WiFi
2013-11-14 12:45
746 查看
以下方法在2.1中测试成功。理论上2.1以上应该也可以。不过2.1以后的Android版本中已经有提供了相关方法,详见
android.provider.Settings.Secure类。
记得在
AndroidManifest.xml中声明相关权限:
<uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permissionandroid:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permissionandroid:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permissionandroid:name="android.permission.CHANGE_WIFI_STATE"/>
1.打开WiFi最简单,直接调用系统的方法即可:
/**
*WIFI网络开关
*/
privatevoidtoggleWiFi(Contextcontext,booleanenabled){
WifiManagerwm=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wm.setWifiEnabled(enabled);
}
2.打开移动网络比较麻烦,系统没有直接提供开放的方法,只在ConnectivityManager类中有一个不可见的setMobileDataEnabled方法,查看源代码发现,它是调用IConnectivityManager类中的setMobileDataEnabled(boolean)方法。由于方法不可见,只能采用反射来调用:
/**
*移动网络开关
*/
privatevoidtoggleMobileData(Contextcontext,booleanenabled){
ConnectivityManagerconMgr=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
Class<?>conMgrClass=null;//ConnectivityManager类
FieldiConMgrField=null;//ConnectivityManager类中的字段
ObjectiConMgr=null;//IConnectivityManager类的引用
Class<?>iConMgrClass=null;//IConnectivityManager类
MethodsetMobileDataEnabledMethod=null;//setMobileDataEnabled方法
try{
//取得ConnectivityManager类
conMgrClass=Class.forName(conMgr.getClass().getName());
//取得ConnectivityManager类中的对象mService
iConMgrField=conMgrClass.getDeclaredField("mService");
//设置mService可访问
iConMgrField.setAccessible(true);
//取得mService的实例化类IConnectivityManager
iConMgr=iConMgrField.get(conMgr);
//取得IConnectivityManager类
iConMgrClass=Class.forName(iConMgr.getClass().getName());
//取得IConnectivityManager类中的setMobileDataEnabled(boolean)方法
setMobileDataEnabledMethod=iConMgrClass.getDeclaredMethod("setMobileDataEnabled",Boolean.TYPE);
//设置setMobileDataEnabled方法可访问
setMobileDataEnabledMethod.setAccessible(true);
//调用setMobileDataEnabled方法
setMobileDataEnabledMethod.invoke(iConMgr,enabled);
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}catch(NoSuchFieldExceptione){
e.printStackTrace();
}catch(SecurityExceptione){
e.printStackTrace();
}catch(NoSuchMethodExceptione){
e.printStackTrace();
}catch(IllegalArgumentExceptione){
e.printStackTrace();
}catch(IllegalAccessExceptione){
e.printStackTrace();
}catch(InvocationTargetExceptione){
e.printStackTrace();
}
}
3.打开GPS也比较麻烦,与打开移动网络一样,没有直接的方法。在网上搜索了一下,据说采用反射去调用系统的方法仍然会失败,有网友用另外一条路实现了同样的功能:
/**
*<p>GPS开关
*<p>当前若关则打开
*<p>当前若开则关闭
*/
privatevoidtoggleGPS(){
IntentgpsIntent=newIntent();
gpsIntent.setClassName("com.android.settings","com.android.settings.widget.SettingsAppWidgetProvider");
gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
gpsIntent.setData(Uri.parse("custom:3"));
try{
PendingIntent.getBroadcast(this,0,gpsIntent,0).send();
}catch(CanceledExceptione){
e.printStackTrace();
}
}
android.provider.Settings.Secure类。
记得在
AndroidManifest.xml中声明相关权限:
1.打开WiFi最简单,直接调用系统的方法即可:
*WIFI网络开关
*/
privatevoidtoggleWiFi(Contextcontext,booleanenabled){
WifiManagerwm=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wm.setWifiEnabled(enabled);
}
2.打开移动网络比较麻烦,系统没有直接提供开放的方法,只在ConnectivityManager类中有一个不可见的setMobileDataEnabled方法,查看源代码发现,它是调用IConnectivityManager类中的setMobileDataEnabled(boolean)方法。由于方法不可见,只能采用反射来调用:
*移动网络开关
*/
privatevoidtoggleMobileData(Contextcontext,booleanenabled){
ConnectivityManagerconMgr=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
Class<?>conMgrClass=null;//ConnectivityManager类
FieldiConMgrField=null;//ConnectivityManager类中的字段
ObjectiConMgr=null;//IConnectivityManager类的引用
Class<?>iConMgrClass=null;//IConnectivityManager类
MethodsetMobileDataEnabledMethod=null;//setMobileDataEnabled方法
try{
//取得ConnectivityManager类
conMgrClass=Class.forName(conMgr.getClass().getName());
//取得ConnectivityManager类中的对象mService
iConMgrField=conMgrClass.getDeclaredField("mService");
//设置mService可访问
iConMgrField.setAccessible(true);
//取得mService的实例化类IConnectivityManager
iConMgr=iConMgrField.get(conMgr);
//取得IConnectivityManager类
iConMgrClass=Class.forName(iConMgr.getClass().getName());
//取得IConnectivityManager类中的setMobileDataEnabled(boolean)方法
setMobileDataEnabledMethod=iConMgrClass.getDeclaredMethod("setMobileDataEnabled",Boolean.TYPE);
//设置setMobileDataEnabled方法可访问
setMobileDataEnabledMethod.setAccessible(true);
//调用setMobileDataEnabled方法
setMobileDataEnabledMethod.invoke(iConMgr,enabled);
}catch(ClassNotFoundExceptione){
e.printStackTrace();
}catch(NoSuchFieldExceptione){
e.printStackTrace();
}catch(SecurityExceptione){
e.printStackTrace();
}catch(NoSuchMethodExceptione){
e.printStackTrace();
}catch(IllegalArgumentExceptione){
e.printStackTrace();
}catch(IllegalAccessExceptione){
e.printStackTrace();
}catch(InvocationTargetExceptione){
e.printStackTrace();
}
}
3.打开GPS也比较麻烦,与打开移动网络一样,没有直接的方法。在网上搜索了一下,据说采用反射去调用系统的方法仍然会失败,有网友用另外一条路实现了同样的功能:
*<p>GPS开关
*<p>当前若关则打开
*<p>当前若开则关闭
*/
privatevoidtoggleGPS(){
IntentgpsIntent=newIntent();
gpsIntent.setClassName("com.android.settings","com.android.settings.widget.SettingsAppWidgetProvider");
gpsIntent.addCategory("android.intent.category.ALTERNATIVE");
gpsIntent.setData(Uri.parse("custom:3"));
try{
PendingIntent.getBroadcast(this,0,gpsIntent,0).send();
}catch(CanceledExceptione){
e.printStackTrace();
}
}
相关文章推荐
- Android中使用代码开关GPS、移动网络(GPRS)、WiFi
- Android中使用代码开关GPS、移动网络(GPRS)、WiFi
- Android在代码中打开Wifi、移动网络和GPS
- Android中使用代码控制Wifi及数据连接网络开关
- Android中使用代码控制Wifi及数据连接网络开关
- Android在代码中打开Wifi、移动网络和GPS
- Android在代码中打开Wifi、移动网络和GPS
- Android在代码中打开Wifi、移动网络和GPS
- Android在代码中打开Wifi、移动网络和GPS
- Android在代码中打开Wifi、移动网络和GPS
- [置顶] Android在代码中打开Wifi、移动网络和GPS
- android使用代码实现gps,gprs,wifi打开与关闭
- 代码中开关Wifi、移动网络和GPS
- Android用代码实现GPS的开启/关闭功能+网络开关
- Android打开WIFI或者移动网络的代码实现
- Android打开WIFI或者移动网络的代码实现
- Android下同时使用WIFI与移动网络时保证优先使用移动网络
- Android判断GPS、wifi、移动网络是否可用的探知函数
- Android打开WIFI或者移动网络的代码实现
- Android 判断网络是GPRS还是WIFI