【问题标题】:How to turn GPS on and off?如何打开和关闭 GPS?
【发布时间】:2013-12-19 08:31:05
【问题描述】:

Android 是否允许以编程方式打开和关闭 GPS?

我正在使用下面的代码 [functions turnGPSon() & turnGPSoff()] (from here)

我正在使用“GPSStatus()”函数来检测 GPS 是否开启。

两个问题:

  1. turnGPSoff() 不工作。 turnGPSon() 能够打开 GPS('我认为',因为我可以在左上角的状态图标中看到 GPS 符号,但是控制中心 [galaxy s3] 中的 GPS 按钮没有突出显示,并且我得到了准确的定位)

  2. GPSStatus() 总是返回 false - 我检查的方法是否正确?

我的目标只是以编程方式打开和关闭 GPS? (是不是Android不允许通过编程方式开启和关闭GPS?答案here中提到)

    private boolean GPSStatus() {
    ContentResolver contentResolver = getBaseContext().getContentResolver();
    boolean gpsStatus = Settings.Secure.isLocationProviderEnabled(contentResolver,    LocationManager.GPS_PROVIDER);
    return gpsStatus;
    }


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

    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"));
        this.sendBroadcast(poke);
    }
}



// automatic turn off the gps
public void turnGPSOff()
{

    myLogger.displayMsgs("in turnGPSoff()", true, true);
    Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
    intent.putExtra("enabled", false);
    sendBroadcast(intent);

}

【问题讨论】:

    标签: android gps location


    【解决方案1】:

    重复,原始here

    答案:

    (您需要 root 访问权限才能完全控制,所以这几乎是不可能的)

    原答案:

    1. 启用/禁用 GPS 在设计上是异步过程。您可以侦听禁用和重新启用 GPS 的尝试。但是您无法阻止实际禁用。 GPS 将关闭一秒钟或几秒钟。 或者,您必须设计自己的 ROM,实现某种 GPS 锁定,仅适用于您自己的应用程序。

    要获得超级管理员权限,您需要 a) root 和 b) 设计一个安装在 /system/app 目录中的小型存根应用程序。 作为上述所有示例,您可以从https://github.com/sms2000/GPSToggler 获取我的 GPSToggler。它或多或少地工作,可以根据您的需要采用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多