我知道在不进入设置的情况下不可能打开本地化,但是有什么方法可以让用户允许在一天中的特定时间打开本地化吗?
最佳答案
打开 gps
和获取 gps 修复
是两件独立的事情。所以你必须获得用户的许可或请求用户在关闭时打开gps,但你不需要询问gps何时打开并且你需要一个位置。一直打开 GPS 不会耗尽电池电量。但是如果任何程序每次都会请求位置。
所以在您的应用中您可以做的是-
- 每次都打开 Gps。但不要请求位置。
- 同时请记住注销 LocationListener 或 不使用时的 googleApiClient,这样它就不会耗尽设备电池。
代码示例:
检查并打开 GPS:
private void CheckEnableGPS(){
String provider = Settings.Secure.getString(getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if(!provider.equals("")){
//GPS Enabled
Toast.makeText(AndroidEnableGPS.this, "GPS Enabled: " + provider,
Toast.LENGTH_LONG).show();
}else{
Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);
startActivity(intent);
}
}
请求当前位置:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
@Override
public void onLocationChanged(Location location) {
Log.i("log","Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
}
you can also use Google Fused API
其他资源:
关于java - 在用户明确同意的情况下,是否可以安排时间打开位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38104029/