gpt4 book ai didi

java - 如何使用设备管理员获取 Android 上的当前位置(纬度、经度)?

转载 作者:行者123 更新时间:2023-12-01 16:53:24 25 4
gpt4 key购买 nike

当尝试解锁手机失败时,我想获取当前位置(纬度、经度)

这是获取最后位置的类

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationManager;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

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.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;

public class LocationSample {

FusedLocationProviderClient mFusedLocationClient;

String latitudeText = "0", longitudeText = "0";

Context context;

LocationSample(Context ctx)
{
context = ctx;
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(ctx);
}
@SuppressLint("MissingPermission")
public void getLastLocation(){

if (checkPermissions()) {
if (isLocationEnabled()) {
mFusedLocationClient.getLastLocation().addOnCompleteListener(
new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
Location location = task.getResult();
if (location == null) {
requestNewLocationData();
} else {
latitudeText = String.valueOf(location.getLatitude()) ;
longitudeText = String.valueOf(location.getLongitude()) ;

}
}
}
);
} else {
/*Toast.makeText(this, "Turn on location", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);*/
Log.d("LocationSample", "GPS is disabled !!!");
Toast.makeText(context, "GPS is disabled !!!", Toast.LENGTH_LONG).show();
}
} else {
Log.d("LocationSample", "there is no permession for location");
Toast.makeText(context, "there is no permession for location", Toast.LENGTH_LONG).show();
}
}

private LocationCallback mLocationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Location location = locationResult.getLastLocation();
latitudeText = String.valueOf(location.getLatitude());
longitudeText = String.valueOf(location.getLongitude());
}
};

@SuppressLint("MissingPermission")
private void requestNewLocationData(){

Toast.makeText(context, "requestNewLocationData", Toast.LENGTH_LONG).show();
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(0);
mLocationRequest.setFastestInterval(0);
mLocationRequest.setNumUpdates(1);

mFusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
mFusedLocationClient.requestLocationUpdates(
mLocationRequest, mLocationCallback,
Looper.myLooper()
);

}


private boolean checkPermissions() {
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission( context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
return true;
}
return false;
}


private boolean isLocationEnabled() {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(
LocationManager.NETWORK_PROVIDER
);
}

public String getLatitudeText() {
return latitudeText;
}

public String getLongitudeText() {
return longitudeText;
}

}

我在 DeviceAdminSampleReceiver 类的函数 onPasswordFailed() 中调用函数 getLastLocation() 、 getLatitudeText() 和 getLongitudeText() ,但总是得到 Latitude = 0/Longitude = 0

 @SuppressLint("LongLogTag")
@Override
public void onPasswordFailed(Context context, Intent intent) {
super.onPasswordFailed(context, intent);

Log.d(TAG, "onPasswordFailed");

int attempts =0;
DevicePolicyManager DevicePolicyManager=
(DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);
if(DevicePolicyManager != null)
attempts = DevicePolicyManager.getCurrentFailedPasswordAttempts();


//GetLastLocation
LocationSample detectedLocation = new LocationSample(context);
detectedLocation.getLastLocation();
String Latitude = "Latitude = " + detectedLocation.getLatitudeText();
String Longitude = "Longitude = " + detectedLocation.getLongitudeText();

Log.d(TAG, Latitude);
Log.d(TAG, Longitude);

}

最佳答案

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;

public class AppLocationService extends Service implements LocationListener {

protected LocationManager locationManager;
Location location;

private static final long MIN_DISTANCE_FOR_UPDATE = 10;
private static final long MIN_TIME_FOR_UPDATE = 1000 * 60 * 2;

public AppLocationService(Context context) {
locationManager = (LocationManager) context
.getSystemService(LOCATION_SERVICE);
}

@SuppressLint("MissingPermission")
public Location getLocation(String provider) {
if (locationManager.isProviderEnabled(provider)) {
locationManager.requestLocationUpdates(provider,
MIN_TIME_FOR_UPDATE, MIN_DISTANCE_FOR_UPDATE, this);
if (locationManager != null) {
location = locationManager.getLastKnownLocation(provider);
return location;
}

}
return null;
}

@Override
public void onLocationChanged(Location location) {
}

@Override
public void onProviderDisabled(String provider) {
}

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}

@Override
public IBinder onBind(Intent arg0) {
return null;
}

}

调用位置服务:

AppLocationService appLocationService = new AppLocationService(context);
Location gpsLocation = appLocationService
.getLocation(LocationManager.GPS_PROVIDER);
if (gpsLocation != null) {
double latitude = gpsLocation.getLatitude();
double longitude = gpsLocation.getLongitude();
String result = "Latitude: " + gpsLocation.getLatitude() +
" Longitude: " + gpsLocation.getLongitude();
Log.d(TAG, "resulat location = " + result);

关于java - 如何使用设备管理员获取 Android 上的当前位置(纬度、经度)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61639609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com