【发布时间】:2017-11-10 05:55:06
【问题描述】:
我正在构建一个应用程序,我必须在其中不断保存用户的位置,然后将其发送到服务器。为此,我在服务中使用 FusedLocationApis。我第一次要求用户打开位置。 它运行良好。
现在,如果用户无意中关闭了该位置,该怎么办。我该怎么通知他?开发人员是否必须处理这个问题,或者他们只是把它留给用户?
我想为此给他一个 android 通知。为此,我需要不断检查我们在服务中启用的位置。我有一个工作代码(函数 checkHighAccuracyLocationMode)。但是我应该在哪里做这个检查(checkHighAccuracyLocationMode)? 每次在此服务中启动哪个功能?或者,如果我能获得一个在用户关闭其位置时启动的功能?
这是我的服务:
public class locationUpdatesService extends Service implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private Location mLastLocation;
private Location mCurrentLocation;
private String mLastUpdateTime;
private dataBaseHandler db_helper;
@Override
public int onStartCommand(Intent intent, int flags, int startId){
initialise();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onCreate() {
super.onCreate();
db_helper = new dataBaseHandler(getApplicationContext(),dataBaseHandler.DATABASE_NAME,null,1);
}
@Override
public void onDestroy() {
stopLocationUpdates();
singletGoogleClientApi.setinstancenull();
singletLocationRequest.setinstancenull();
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void initialise(){
Log.e("ghgdhu","qaws");
if (singletGoogleClientApi.getinstance().getGoogleApiCLient() == null) {
singletGoogleClientApi.getinstance().setmSingletGoogleApiClient(new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build());
singletGoogleClientApi.getinstance().getGoogleApiCLient().connect();
createLocationRequest();
}
if(!singletGoogleClientApi.getinstance().getGoogleApiCLient().isConnected()){
singletGoogleClientApi.getinstance().getGoogleApiCLient().connect();
createLocationRequest();
}
}
protected void createLocationRequest() {
if(singletLocationRequest.getinstance().getLocationRequest() == null) {
singletLocationRequest.getinstance().setSingletLocationRequest(new LocationRequest()
.setInterval(10000)
.setFastestInterval(5000).setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY));
}
}
public static boolean checkHighAccuracyLocationMode(Context context) {
int locationMode = 0;
String locationProviders;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
//Equal or higher than API 19/KitKat
try {
locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
if (locationMode == Settings.Secure.LOCATION_MODE_HIGH_ACCURACY){
return true;
}
} catch (Settings.SettingNotFoundException e) {
e.printStackTrace();
}
}else{
//Lower than API 19
locationProviders = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (locationProviders.contains(LocationManager.GPS_PROVIDER) && locationProviders.contains(LocationManager.NETWORK_PROVIDER)){
return true;
}
}
return false;
}
@Override
public void onLocationChanged(Location location) {
Log.e("qazxsw","inONLocationCHanged");
mCurrentLocation = location;
Log.e("loc : ",Double.toString(mCurrentLocation.getLatitude()));
Toast.makeText(this, "My Location: "+Double.toString(mCurrentLocation.getLatitude())+ " , " + Double.toString(mCurrentLocation.getLongitude()),
Toast.LENGTH_SHORT).show();
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
if(!checkHighAccuracyLocationMode(getBaseContext())){
Log.e("turned OFF","LOCATION");
}
else {
Log.e("ONNNNN","LOCATION");
}
db_helper.Insert(mLastUpdateTime,mCurrentLocation.getLatitude(),mCurrentLocation.getLongitude());
}
protected void stopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(singletGoogleClientApi.
getinstance().getGoogleApiCLient(), this);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
startLocationUpdates();
}
protected void startLocationUpdates() {
try {
if(singletLocationRequest.getinstance().getLocationRequest()!=null &&
singletGoogleClientApi.getinstance().getGoogleApiCLient()!=null){
Log.e("requesting","yES BRO");
LocationServices.FusedLocationApi.requestLocationUpdates(
singletGoogleClientApi.getinstance().getGoogleApiCLient(), singletLocationRequest.getinstance().getLocationRequest(), this);
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
singletGoogleClientApi.getinstance().getGoogleApiCLient());
}
else {
initialise();
}
}
catch (SecurityException e) {
e.getStackTrace();
}
}
@Override
public void onConnectionSuspended(int i) {
Log.e("ON CONNECTION SUSPENDED","PETROL");
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.e("COnnection ","DIESEL");
}
}
【问题讨论】:
-
覆盖方法 onProviderDisabled 。这是您为场景编写逻辑的地方。详情请参考developer.android.com/reference/android/location/…。
-
@Jimmy 我之前尝试过覆盖 onProviderEnabled/Disabled 方法。但我认为以我目前的实施方式,我将无法这样做。
-
@Jimmy 我想如果我通过 LocationManager 而不是通过 goooglefusedAPI 去实现所有方法。你怎么看?
-
如果您能够覆盖 onLocationChanged ,那么您也可以为 onProviderDisabled 提供实现。无需更改任何内容,您已经在实现 LocationListener,只需覆盖该方法即可。
标签: android location fusedlocationproviderapi