简体   繁体   English

如何开启和关闭GPS?

[英]How to turn GPS on and off?

Does Android allow GPS to be turned on and off programmatically? Android是否允许以编程方式打开和关闭GPS?

I am using the code [functions turnGPSon() & turnGPSoff()] below ( from here ) 我正在使用下面的代码[功能turnGPSon()和turnGPSoff()]( 从此处开始

I am using using the function "GPSStatus()" to detect if GPS is on or not. 我正在使用函数“ GPSStatus()”来检测GPS是否打开。

Two problems: 两个问题:

  1. turnGPSoff() is NOT working. turnGPSoff()无法正常工作。 turnGPSon() is able to turn the GPS on ('I think' because I can see the GPS symbol in the top left status icon, but GPS button in control center [galaxy s3] is not highlighted though, and I am getting accurate loation) turnGPSon()能够打开GPS(“我认为”,因为我可以在左上角的状态图标中看到GPS符号,但是控制中心[galaxy s3]中的GPS按钮并未突出显示,并且我得到了准确的定位)

  2. GPSStatus() is ALWAYS returning false - am I checking the correct way? GPSStatus()总是返回false-我在检查正确的方法吗?

My goal is only to turn GPS on and off pro grammatically? 我的目标只是以编程方式打开和关闭GPS吗? (Is it true that Android does not allow GPS to be turned on and off programmatically? as mentioned in the answer here ) (这是真的,Android的不允许GPS进行开启和关闭程序?作为在答复中提到在这里

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

}

Duplicate, original here 重复,原始在这里

Answer: 回答:

(You would need rootaccess for full control so it's pretty much not possible) (您需要rootaccess才能完全控制,因此几乎是不可能的)

Original answer: 原始答案:

  1. Enabling/disabling GPS is asynchronous process by design. 启用/禁用GPS是设计使然的异步过程。 You can listen for the attempt to disable and reenable GPS back. 您可以侦听禁用和重新启用GPS的尝试。 But you cannot prevent the actual disabling. 但是您不能阻止实际的禁用。 The GPS will be off for a second or few. GPS将关闭一秒钟或几秒钟。 Or you have to design your own ROM implementing some kind of lock for GPS available only to your own applications. 或者,您必须设计自己的ROM,以实现仅适用于您自己的应用程序的GPS锁定。

To obtain the SuperAdmin privileges your need a) root and b) to design a small stub application installed in /system/app directory. 要获得SuperAdmin特权,您需要a)根用户和b)设计安装在/ system / app目录中的小型存根应用程序。 As a sample of all above you may take my GPSToggler from https://github.com/sms2000/GPSToggler . 作为上述所有示例,您可以从https://github.com/sms2000/GPSToggler中获取我的GPSToggler。 It works more or less and can be adopted for your needs. 它或多或少地起作用,并且可以满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM