MockLocationService
package com.pxc.su;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.SystemClock;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
public class MockLocationService extends Service implements LocationListener {
private static final String TAG = "MockLocationService";
LocationManager locationManager;
double longitude = 114.059490;
double latitude = 22.657641;
Thread thread;
MyHandler mHandler;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
initMockLocationProvider();
mHandler = new MyHandler();
thread = new Thread(new Runnable() {
int count = 1000;
@Override
public void run() {
while (true) {
for (int i = 1; i < count; i++) {
float in = (float) (i * 0.0001);
Point p = new Point();
p.latitude = latitude + in;
p.longitude = longitude + in;
Message msg = mHandler.obtainMessage(MyHandler.SET_LOCATION);
msg.obj = p;
msg.sendToTarget();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
});
thread.start();
}
private void initMockLocationProvider() {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.addTestProvider(LocationManager.GPS_PROVIDER, false, true, false, false, false, false, false, 0, 5);
locationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, true);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
locationManager.addTestProvider(LocationManager.NETWORK_PROVIDER, false, true, false, false, false, false, false, 0, 5);
locationManager.setTestProviderEnabled(LocationManager.NETWORK_PROVIDER, true);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
public void setLocation(double latitude, double longitude) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Location loc = new Location(LocationManager.GPS_PROVIDER);
loc.setTime(System.currentTimeMillis());
loc.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
loc.setLatitude(latitude);
loc.setLongitude(longitude);
loc.setAccuracy(3);
locationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, loc);
Log.i(TAG, "setLocation 1: " + loc);
loc.setProvider(LocationManager.NETWORK_PROVIDER);
locationManager.setTestProviderLocation(LocationManager.NETWORK_PROVIDER, loc);
Log.i(TAG, "setLocation 2: " + loc);
}
class Point {
double latitude;
double longitude;
}
class MyHandler extends Handler {
public static final int SET_LOCATION = 0;
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case SET_LOCATION:
Point point = (Point) msg.obj;
setLocation(point.latitude, point.longitude);
break;
default:
break;
}
}
}
@Override
public void onLocationChanged(@NonNull Location location) {
Log.i(TAG, "onLocationChanged location:" + location);
}
@Override
public void onFlushComplete(int requestCode) {
}
@Override
public void onProviderEnabled(@NonNull String provider) {
}
@Override
public void onProviderDisabled(@NonNull String provider) {
}
}
MainActivity
package com.pxc.su;
import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.pxc.su.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
TextView tv = binding.sampleText;
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, MockLocationService.class);
startService(intent);
}
});
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION, "android.permission.ACCESS_MOCK_LOCATION"}, 0);
}