问题描述
我正在开发一个应用程序,当我开始活动时,我想启用GPS,并且在退出应用程序时想禁用GPS.我可以通过我的程序做到吗.
任何帮助将不胜感激.
推荐答案
这是代码...
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); } }
其他推荐答案
您可以使用
开始启动GPSLocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationListener locationListener = new CTLocationListener(); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1.0f, locationListener);
停止GPS提供商使用
locationManager.removeUpdates(locationListener);
其他推荐答案
我在所有网站上都研究了此主题.我找到了下面的代码.我尝试了它的工作.
方法1
Enable GPS Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); intent.putExtra("enabled", true); sendBroadcast(intent); Disable GPS Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); intent.putExtra("enabled", false); sendBroadcast(intent);
方法2
// If GPS is OFF private void turnGPSOn(){ String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if(!provider.contains("gps")){ final Intent intent = new Intent(); intent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); intent.addCategory(Intent.CATEGORY_ALTERNATIVE); intent.setData(Uri.parse("3")); sendBroadcast(intent); } } // If GPS is ON private void turnGPSOff(){ String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if(provider.contains("gps")){ final Intent intent = new Intent(); intent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); intent.addCategory(Intent.CATEGORY_ALTERNATIVE); intent.setData(Uri.parse("3")); sendBroadcast(intent); } }
更多信息您可以访问此网站: http://androidmaterial.blogspot.com/2012/04/how-to-to-enabledisable-gps-in-android.html
问题描述
I am developing an application wherein I want to enable GPS, when I start my activity and I want to disable GPS when I exit my application. Can I do it through my program..
Any help would be appreciated.
推荐答案
Here is the code...
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); } }
其他推荐答案
You can start the GPS by using
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); LocationListener locationListener = new CTLocationListener(); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1.0f, locationListener);
To stop the gps provider use
locationManager.removeUpdates(locationListener);
其他推荐答案
I researched about this subject on all web sites. and I found this code like below. I tried its working.
Method-1
Enable GPS Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); intent.putExtra("enabled", true); sendBroadcast(intent); Disable GPS Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE"); intent.putExtra("enabled", false); sendBroadcast(intent);
Method-2
// If GPS is OFF private void turnGPSOn(){ String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if(!provider.contains("gps")){ final Intent intent = new Intent(); intent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); intent.addCategory(Intent.CATEGORY_ALTERNATIVE); intent.setData(Uri.parse("3")); sendBroadcast(intent); } } // If GPS is ON private void turnGPSOff(){ String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); if(provider.contains("gps")){ final Intent intent = new Intent(); intent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); intent.addCategory(Intent.CATEGORY_ALTERNATIVE); intent.setData(Uri.parse("3")); sendBroadcast(intent); } }
Fore more information you can visit this web site : http://androidmaterial.blogspot.com/2012/04/how-to-enabledisable-gps-in-android.html