我正在开发一个超级克隆应用程序,当我将应用程序安装在尚未安装的设备上时,我面临的问题如上所述,它会获取我当前的位置并显示甚至跟踪我的动作,但是当我注销或退出时该应用程序甚至没有显示我当前的位置。它只是展示了非洲大陆。 下面的代码,应该获取位置的活动
package com.example.uber_clone;
import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentActivity;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.location.Location;
import android.location.LocationListener;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.example.uber_clone.DriverLogin;
import com.example.uber_clone.R;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApi;
import com.google.android.gms.common.api.GoogleApiClient;
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.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MapStyleOptions;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import static com.google.android.gms.location.LocationServices.*;
public class Map_Driver extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private Location mLastLocation;
private LocationRequest mLocationRequest;
private FusedLocationProviderClient mFusedLocationClient;
private int status = 0;
private String customerId = "", destination;
private LatLng destinationLatLng, pickupLatLng;
private float rideDistance;
Button logout_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map__driver);
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
logout_btn = findViewById(R.id.logout_btn);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
logout_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
Intent intent = new Intent(Map_Driver.this, DriverLogin.class);
startActivity(intent);
finish();
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if(VERSION.SDK_INT >= VERSION_CODES.M){
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
}else{
checkLocationPermission();
}
}
}
LocationCallback mLocationCallback = new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult) {
for(Location location : locationResult.getLocations()){
if(getApplicationContext()!=null){
// if(!customerId.equals("") && mLastLocation!=null && location != null){
// rideDistance += mLastLocation.distanceTo(location)/1000;
// }
mLastLocation = location;
LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(18));
// String userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
// DatabaseReference refAvailable = FirebaseDatabase.getInstance().getReference("driversAvailable");
// DatabaseReference refWorking = FirebaseDatabase.getInstance().getReference("driversWorking");
// GeoFire geoFireAvailable = new GeoFire(refAvailable);
// GeoFire geoFireWorking = new GeoFire(refWorking);
//
// switch (customerId){
// case "":
// geoFireWorking.removeLocation(userId);
// geoFireAvailable.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()));
// break;
//
// default:
// geoFireAvailable.removeLocation(userId);
// geoFireWorking.setLocation(userId, new GeoLocation(location.getLatitude(), location.getLongitude()));
// break;
// }
}
}
}
};
@Override
protected void onResume() {
super.onResume();
}
private void checkLocationPermission() {
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
new AlertDialog.Builder(this)
.setTitle("Grant Location Permissions")
.setMessage("Please Grant Location Permission, Or Else App Might Not Work")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
ActivityCompat.requestPermissions(Map_Driver.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
})
.create()
.show();
}
else{
ActivityCompat.requestPermissions(Map_Driver.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch(requestCode){
case 1:{
if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
mMap.setMyLocationEnabled(true);
}
} else{
Toast.makeText(getApplicationContext(), "Please provide the permission", Toast.LENGTH_LONG).show();
}
break;
}
}
}
}
*我是新手,非常感谢任何帮助。提前致谢。