【问题标题】:How to enable/disable gps and mobile data in android programmatically?如何以编程方式在android中启用/禁用gps和移动数据?
【发布时间】:2013-05-21 02:39:49
【问题描述】:

我希望我的应用程序能够以编程方式启用/禁用 gps 和移动数据,因为有许多应用程序,例如 tasker、profile flow、lookout extra 可以做到这一点,所以我搜索了它,但没有找到任何有用的示例。下面的代码,但他们没有工作。

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);
 }
} 

【问题讨论】:

  • 你用什么安卓版本进行测试?
  • 安卓 2.3 和 4.1.2
  • 试试下面的代码,让我知道它是否有效。
  • 它的工作谢谢,但它正在做的是打开服务,所以如果使用我的应用程序启动它,状态栏顶部会出现一个图标,它正在搜索 gps.... ..如果我什至手动或从应用程序关闭它,它不会从那里去,在 android 4.1.2 上它没有打开状态栏中的 gps 图标,但它启动了服务,但在 android 2.3 中它打开了图标,也启动了服务
  • 忘记 GPS...您至少可以禁用移动数据吗?如果是,如何...代码(GPS 除外)如何工作?另外,请提及其他所需的更改,例如命名空间等......

标签: java android gps settings


【解决方案1】:

我希望我的应用程序能够启用/禁用 gps

幸运的是,出于明显的隐私原因,这是不可能的。虽然有一些黑客(例如您问题中的那个)曾经有效,但这些安全漏洞已得到修复。

因为有许多应用程序,例如 tasker、profile flow、lookout extra 可以做到这一点

欢迎您提供证据证明这些应用中的任何一个都可以在现代版本的 Android 上执行此操作。

【讨论】:

  • 我是 Profile Flow 的开发者。关于这个讨论,在最新版本的 android.. 中控制/更改 GPS 设置是不可能的。除非它是 root!
  • 既然问题也是关于移动网络的,既然你没有提到它,我认为这个功能仍然适用于我们所有的平民......:)?
  • @TacB0sS:我对移动数据一无所知。
  • 稍后我会试一试,然后发布回复...谢谢
  • 基于同样的概念,设备语言环境更改 api 也受到限制,自 4.2 起
【解决方案2】:

试试这个开启gps

public void turnGPSOn()
{
     Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
     intent.putExtra("enabled", true);
     this.ctx.sendBroadcast(intent);

    String provider = Settings.Secure.getString(ctx.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")); 
        this.ctx.sendBroadcast(poke);


    }
}

用于关闭 GPS,

public void turnGPSOff()
{
    String provider = Settings.Secure.getString(ctx.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")); 
        this.ctx.sendBroadcast(poke);
    }
}

对于移动数据,您是否在清单中添加了权限?如果没有尝试添加并检查它。

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

如果一切正常,请告诉我。

【讨论】:

  • 它的工作谢谢,但它正在做的是打开服务,所以如果使用我的应用程序启动它,状态栏顶部会出现一个图标,它正在搜索 gps.... ..如果我什至手动或从应用程序关闭它,它不会从那里去,在 android 4.1.2 上它没有打开状态栏中的 gps 图标,但它启动了服务,但在 android 2.3 中它打开了图标,也启动了服务。
  • 幸运的是,您的 GPS 黑客攻击依赖于一个已经修复了一段时间的安全漏洞。
  • @CommonsWare 感谢提及,我会在以后的场景中更正它。
  • 我试过了,它只是显示“正在搜索 GPS”,但实际上并没有打开它
  • 它在通知栏上显示gps图标,但实际上并没有打开gps
猜你喜欢
  • 2011-06-10
  • 2013-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多