无法在后台获取位置更新?

时间:2019-05-27 03:38:43

标签: java android

我正在尝试使用FusedLocationProviderClient来获得后台服务,但是它无法在后台运行。我如何使其在后台工作? 如果这不是最好的方法,请您给我最好的方法。

这是我的主要活动

 package com.professionalandroid.apps.whereami;

import android.Manifest;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentSender;
import android.location.Location;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.common.api.ResolvableApiException;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResponse;
import com.google.android.gms.location.LocationSettingsStates;
import com.google.android.gms.location.LocationSettingsStatusCodes;
import com.google.android.gms.location.SettingsClient;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;

import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static 
android.support.v4.content.PermissionChecker.PERMISSION_GRANTED;

public class WhereAmIActivity extends AppCompatActivity {

public static final String TAG = "WhereAmIActivity";
private static final String ERROR_MSG = "Google Play services are 
unavailable.";
private static final int LOCATION_PERMISSION_REQUEST = 1;
private static final int REQUEST_CHECK_SETTINGS = 2;

private TextView mTextView;
private LocationRequest mLocationRequest;

LocationCallback mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
  Location location = locationResult.getLastLocation();
  if (location != null) {
    updateTextView(location);
  }
}
 };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_where_am_i);

mTextView = findViewById(R.id.myLocationText);

GoogleApiAvailability availability = GoogleApiAvailability.getInstance();

int result = availability.isGooglePlayServicesAvailable(this);
if (result != ConnectionResult.SUCCESS) {
  if (!availability.isUserResolvableError(result)) {
    Toast.makeText(this, ERROR_MSG, Toast.LENGTH_LONG).show();
  }
}

mLocationRequest = new LocationRequest()
                     .setInterval(5000)
                     .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
 }

 @Override
 protected void onStart() {
 super.onStart();

// Check if we have permission to access high accuracy fine location.
int permission = ActivityCompat.checkSelfPermission(this,
  ACCESS_FINE_LOCATION);

// If permission is granted, fetch the last location.
if (permission == PERMISSION_GRANTED) {
  getLastLocation();
} else {
  // If permission has not been granted, request permission.
  ActivityCompat.requestPermissions(this,
    new String[]{ACCESS_FINE_LOCATION},
    LOCATION_PERMISSION_REQUEST);
}

// Check of the location settings are compatible with our Location 
Request.
LocationSettingsRequest.Builder builder =
  new LocationSettingsRequest.Builder()
    .addLocationRequest(mLocationRequest);

SettingsClient client = LocationServices.getSettingsClient(this);

Task<LocationSettingsResponse> task = 
client.checkLocationSettings(builder.build());
task.addOnSuccessListener(this,
  new OnSuccessListener<LocationSettingsResponse>() {
    @Override
    public void onSuccess(LocationSettingsResponse
                            locationSettingsResponse) {
      // Location settings satisfy the requirements of the Location 
Request.
      // Request location updates.
      requestLocationUpdates();
    }
  });

task.addOnFailureListener(this, new OnFailureListener() {
  @Override
  public void onFailure(@NonNull Exception e) {
    // Extract the status code for the failure from within the Exception.
    int statusCode = ((ApiException) e).getStatusCode();

    switch (statusCode) {
      case CommonStatusCodes.RESOLUTION_REQUIRED:
        try {
          // Display a user dialog to resolve the location settings
          // issue.
          ResolvableApiException resolvable = (ResolvableApiException) e;
          resolvable.startResolutionForResult(WhereAmIActivity.this,
            REQUEST_CHECK_SETTINGS);
        } catch (IntentSender.SendIntentException sendEx) {
          Log.e(TAG, "Location Settings resolution failed.", sendEx);
        }
        break;
      case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
        // Location settings issues can't be resolved by user.
        // Request location updates anyway.
        Log.d(TAG, "Location Settings can't be resolved.");
        requestLocationUpdates();
        break;
    }
  }
 });
 }

@Override
public void onRequestPermissionsResult(int requestCode,
                                     @NonNull String[] permissions,
                                     @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (requestCode == LOCATION_PERMISSION_REQUEST) {
  if (grantResults[0] != PERMISSION_GRANTED)
    Toast.makeText(this, "Location Permission Denied",
      Toast.LENGTH_LONG).show();
}
}

  private void getLastLocation() {
  FusedLocationProviderClient fusedLocationClient;
  fusedLocationClient =
   LocationServices.getFusedLocationProviderClient(this);

   LocationRequest locationRequest = new LocationRequest()
          .setInterval(3000)
        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
 final int locationRC = 0;
 int flags = PendingIntent.FLAG_UPDATE_CURRENT;
 Intent intent = new Intent(this, MyLocationUpdateReciever.class);
  intent.setAction("com.Android.custom_TUT");
 PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 
 locationRC, 
 intent, flags);
if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION)
    == PERMISSION_GRANTED ||
    ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION)
    == PERMISSION_GRANTED) {



  fusedLocationClient.
  requestLocationUpdates(locationRequest,pendingIntent);
  }
 }

 private void updateTextView(Location location) {
   String latLongString = "No location found";
 if (location != null) {
   double lat = location.getLatitude();
  double lng = location.getLongitude();
  latLongString = "Lat:" + lat + "\nLong:" + lng;
  Toast.makeText(WhereAmIActivity.this, ""+lat+""+lng, 
  Toast.LENGTH_SHORT).show();
 }
 mTextView.setText(latLongString);
  }

  private void requestLocationUpdates() {
  FusedLocationProviderClient fusedLocationClient;
 fusedLocationClient =
        LocationServices.getFusedLocationProviderClient(this);

 LocationRequest locationRequest = new LocationRequest()
        .setInterval(3000)
        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
 final int locationRC = 0;
 int flags = PendingIntent.FLAG_UPDATE_CURRENT;
Intent intent = new Intent(this, MyLocationUpdateReciever.class);
 PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 
  locationRC, 
 intent, flags);
  if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION)
        == PERMISSION_GRANTED ||
        ActivityCompat.checkSelfPermission(this, ACCESS_COARSE_LOCATION)
                == PERMISSION_GRANTED) {



 fusedLocationClient
 .requestLocationUpdates(locationRequest,pendingIntent);
}
}


 @Override
 protected void onActivityResult(int requestCode,
                              int resultCode,Intent data){
final LocationSettingsStates states = 
LocationSettingsStates.fromIntent(data);

if (requestCode == REQUEST_CHECK_SETTINGS) {
  switch (resultCode) {
    case Activity.RESULT_OK:
      // Requested changes made, request location updates.
      requestLocationUpdates();
      break;
    case Activity.RESULT_CANCELED:
      // Requested changes were NOT made.
      Log.d(TAG, "Requested settings changes declined by user.");
      // Check if any location services are available, and if so
      // request location updates.
      if (states.isLocationUsable())
        requestLocationUpdates();
      else
        Log.d(TAG, "No location services available.");
      break;
    default: break;
    }
   }
 }
}

这是广播接收器类

 package com.professionalandroid.apps.whereami;

 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.location.Location;
 import android.util.Log;
 import android.widget.Toast;

 import com.google.android.gms.location.LocationResult;

 public class MyLocationUpdateReciever extends BroadcastReceiver {
 @Override
 public void onReceive(Context context, Intent intent) {
    if(LocationResult.hasResult(intent)){
        LocationResult locationResult = 
 LocationResult.extractResult(intent);
        for(Location location: locationResult.getLocations()){
            Toast.makeText(context, "longitude: = 
"+location.getLongitude()
                    +"latitude: = "+location.getLatitude(), 
  Toast.LENGTH_SHORT).show();
            Log.d("DSK_OPER","backgroud");
        }
     }
  }
 }

我还在清单中添加了用于精确定位和课程定位的权限,并且还添加了服务类。

0 个答案:

没有答案