activate GPS programmatically

你离开我真会死。 提交于 2019-12-05 21:14:52
bvaldivielso

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

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