gpt4 book ai didi

java - 如何在Android上模拟位置?

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

我使用下面的代码来设置模拟位置,而无需“位置”权限。

但是,代码与使用FusedLocationProviderClient的最新Google Play服务版本的Android设备不一致。有时它会按预期工作,有时位置会不断地来回跳动,有时根本不起作用。我也经常因错误java.lang.IllegalArgumentException: Provider “gps” already exists而崩溃。基本上,这是错误的代码,我想在我的应用程序中摆脱它。

我要寻找的是一种完全验证的解决方案,能够像Fake GPS app一样成功地伪造设备的位置(无需位置许可)。我希望用Java重新创建该应用程序。截至2020年5月,我还没有找到可以完全正常运行的在线内容。非常感谢您的帮助。

void setMockLocation() {
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.addTestProvider(LocationManager.GPS_PROVIDER, false, false,
false, false, true, true, true, 0, 5);
locationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);

Location mockLocation = new Location(LocationManager.GPS_PROVIDER);
mockLocation.setLatitude(-33.852); // Sydney
mockLocation.setLongitude(151.211);
mockLocation.setAltitude(10);
mockLocation.setAccuracy(5);
mockLocation.setTime(System.currentTimeMillis());
mockLocation.setElapsedRealtimeNanos(System.nanoTime());
locationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, mockLocation);
}

最佳答案

我查看了Github,并使用了here构建的服务。
以下代码对我有用:

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;

import static android.location.LocationManager.GPS_PROVIDER;

public class LocationService extends Service {

private static final String TAG = LocationService.class.getSimpleName();

private static final String CHANNEL_NAME = "test_ch";
private static final String CHANNEL_ID = "test_id";
private static final String DESCRIPTION = "This is test";

private static final String INTENT_ACTION = "inject_location";

private LocationManager mLocationManager;

@Override
public void onCreate() {
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (manager.getNotificationChannel(CHANNEL_ID) == null) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME,
NotificationManager.IMPORTANCE_HIGH);
channel.setDescription(DESCRIPTION);
manager.createNotificationChannel(channel);
}

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
Notification notification = builder.build();

startForeground(1, notification);

enableProvider();
registerReceiver(mInjectionReceiver, new IntentFilter(INTENT_ACTION));

return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
removeProvider();
}

/**
* Location injection receiver
*
* adb shell am broadcast -a inject_location --esa latlon "48.873792, 2.295028"
*/
private BroadcastReceiver mInjectionReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (!INTENT_ACTION.equals(intent.getAction())) {
return;
}

Bundle extras = intent.getExtras();
if (extras == null) {
return;
}

String[] latlon = extras.getStringArray("latlon");
if (latlon != null && latlon.length == 2) {
updateLocation(Double.parseDouble(latlon[0]), Double.parseDouble(latlon[1]));
}
}
};

/**
* Listener of LocationManagerService
*/
private LocationListener mLocationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.d(TAG, "onLocationChanged " + location);
}

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

@Override
public void onProviderEnabled(String provider) {
}

@Override
public void onProviderDisabled(String provider) {
}
};

private void enableProvider() {
try {
mLocationManager.addTestProvider(
GPS_PROVIDER,
true, false, false, false,
true, true, true,
Criteria.POWER_LOW, Criteria.ACCURACY_FINE
);
} catch (IllegalArgumentException | SecurityException e) {
Log.w(TAG, "addTestProvider" + e.getMessage());
// OK, but I go through
}
try {
mLocationManager.setTestProviderEnabled(GPS_PROVIDER, true);
} catch (IllegalArgumentException | SecurityException e) {
Log.w(TAG, "setTestProviderEnabled" + e.getMessage());
}

}

private void removeProvider() {
mLocationManager.removeUpdates(mLocationListener);
try {
mLocationManager.setTestProviderEnabled(GPS_PROVIDER, false);
mLocationManager.removeTestProvider(GPS_PROVIDER);
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}

private void updateLocation(double latitude, double longitude) {
Location location = makeLocation(latitude, longitude);
Log.d(TAG, "updateLocation " + location);
mLocationManager.setTestProviderLocation(GPS_PROVIDER, location);
}

private Location makeLocation(double latitude, double longitude) {
Location location = new Location(GPS_PROVIDER);
location.setLatitude(latitude);
location.setLongitude(longitude);
location.setTime(System.currentTimeMillis());
location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
location.setAltitude(0.0);
location.setAccuracy(5f);
return location;
}
}

关于java - 如何在Android上模拟位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61787251/

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