2013-12-19 112 views
1

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

我使用下面(from here

我使用的函数 “GPSStatus()” 使用的代码[功能turnGPSon()& turnGPSoff()],以检测是否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); 

} 

回答

0

重复,原来here

答:

(您将需要rootaccess完全控制,这几乎是不可能的)

原来的答复:

  1. 启用/禁用GPS是通过设计的异步处理。您可以聆听尝试禁用并重新启用GPS。但是你不能阻止实际的禁用。 GPS将关闭一秒钟或几秒钟。 或者你必须设计你自己的ROM,为你的应用程序实现某种GPS锁定。

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