activate GPS programmatically

混江龙づ霸主 提交于 2019-12-07 17:28:14

问题


My application needs the GPS to be on, is there any way to check whether GPS is currently enabled or not and if not , then how to enable. I am using android 2.3


回答1:


Android does not allow you to do that. The best thing you can do is checking if the GPS is enabled and if it's not, ask the user to activate it.

Here you can see how to know if the GPS is enabled: How do I find out if the GPS of an Android device is enabled




回答2:


You can do it this way..check it out

private void turnGPSOn()
{
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

if(!provider.contains("gps"))
{ //if gps is off
final Intent enablegps = new Intent();
enablegps.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
enablegps.addCategory(Intent.CATEGORY_ALTERNATIVE);
enablegps.setData(Uri.parse("3")); 
sendBroadcast(enablegps);
}
}

private void turnGPSOff()
{
String provider = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

if(provider.contains("gps"))
{ //if gps is on
final Intent disablegps = new Intent();
disablegps.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
disablegps.addCategory(Intent.CATEGORY_ALTERNATIVE);
disablegps.setData(Uri.parse("3")); 
sendBroadcast(disablegps);
}
}


来源:https://stackoverflow.com/questions/9350641/activate-gps-programmatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!