如何以编程方式启用和禁用GPS

Kri*_*hna 2 gps android

我正在开发一个应用程序,其中我想启用GPS,当我开始我的活动时,我想在退出我的应用程序时禁用GPS.我可以通过我的程序来完成它吗

任何帮助,将不胜感激.

Suj*_*jit 6

这是代码......

 private void turnGPSOn(){   

    String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);   
    if(!provider.contains("gps")){      
        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);  
   }  }    
Run Code Online (Sandbox Code Playgroud)

  • 我认为函数的名称有一个线索. (14认同)
  • 据说,这段代码不再有效,因为它是一个安全漏洞. (3认同)

inc*_*eMe 6

您可以使用启动GPS

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    LocationListener locationListener = new CTLocationListener();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1.0f, locationListener);
Run Code Online (Sandbox Code Playgroud)

停止使用gps提供程序

locationManager.removeUpdates(locationListener);
Run Code Online (Sandbox Code Playgroud)