如何启用/禁用GPS和移动数据在Android的编程? [英] How to enable/disable gps and mobile data in android programmatically?

查看:216
本文介绍了如何启用/禁用GPS和移动数据在Android的编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的应用程序能够启用/禁用GPS和移动数据编程,因为有像塔斯克,轮廓流动许多应用程序,把风额外的可以做到这一点,所以我搜索,但没有发现任何有用的例子,我找到了下面code,但大公didnt工作

 私人无效setMobileDataEnabled(上下文的背景下,布尔启用){
 最后ConnectivityManager赌侠=(ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
 最后类conmanClass =的Class.forName(conman.getClass()的getName());
 最后一个字段iConnectivityManagerField = conmanClass.getDeclaredField(MSERVICE);
 iConnectivityManagerField.setAccessible(真正的);
 最终目标iConnectivityManager = iConnectivityManagerField.get(赌侠);
 最后类iConnectivityManagerClass =的Class.forName(iConnectivityManager.getClass()的getName());
 最后一个方法setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod(setMobileDataEnabled,Boolean.TYPE);
 setMobileDataEnabledMethod.setAccessible(真正的);

 setMobileDataEnabledMethod.invoke(iConnectivityManager,启用);
}

私人无效turnGPSOn(){
 字符串商= Settings.Secure.getString(getContentResolver(),Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

 如果(!provider.contains(GPS)){//如果GPS被禁用
    最终意图捅=新意图();
    poke.setClassName(com.android.settings,com.android.settings.widget.SettingsAppWidgetProvider);
    poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
    poke.setData(Uri.parse(3));
    sendBroadcast(捅);
 }
}

私人无效turnGPSOff(){
 字符串商= Settings.Secure.getString(getContentResolver(),Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

 如果(provider.contains(GPS)){//如果GPS已启用
    最终意图捅=新意图();
    poke.setClassName(com.android.settings,com.android.settings.widget.SettingsAppWidgetProvider);
    poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
    poke.setData(Uri.parse(3));
    sendBroadcast(捅);
 }
}
 

解决方案
  

我想我的应用程序能够启用/禁用GPS

幸好,这是不可能的,由于明显的隐私的原因。虽然有一些黑客,像在你的问题,过去可以正常工作,这些安全漏洞,至今已修复。

  

因为有喜欢塔斯克,轮廓流动许多应用程序,把风额外的可以做到这一点。

欢迎您提供证据表明这些应用程序都可以在Android的现代版本做到这一点。

I want my application to be able to enable/disable gps and mobile data programmatically as there are many apps like tasker, profile flow, lookout extra which can do this so i searched for it but not found any useful example i found the following code but thay didnt worked

private void setMobileDataEnabled(Context context, boolean enabled) {
 final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
 final Class conmanClass = Class.forName(conman.getClass().getName());
 final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
 iConnectivityManagerField.setAccessible(true);
 final Object iConnectivityManager = iConnectivityManagerField.get(conman);
 final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
 final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
 setMobileDataEnabledMethod.setAccessible(true);

 setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
} 

private void turnGPSOn(){
 String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

 if(!provider.contains("gps")){ //if gps is disabled
    final Intent poke = new Intent();
    poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
    poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
    poke.setData(Uri.parse("3")); 
    sendBroadcast(poke);
 }
}

private void turnGPSOff(){
 String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

 if(provider.contains("gps")){ //if gps is enabled
    final Intent poke = new Intent();
    poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
    poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
    poke.setData(Uri.parse("3")); 
    sendBroadcast(poke);
 }
} 

解决方案

I want my application to be able to enable/disable gps

Fortunately, this is not possible, for obvious privacy reasons. While there were some hacks, like the one in your question, that used to work, those security flaws have since been fixed.

as there are many apps like tasker, profile flow, lookout extra which can do this

You are welcome to supply evidence that any of those apps can do this on modern versions of Android.

这篇关于如何启用/禁用GPS和移动数据在Android的编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆