我一直在尝试创建一个Android应用程序,它能够获取和设置Mock Location,而无需在Developer Settings中启用Mock Location。 我已经能够在一定程度上实现这一目标,使用不同的问题而不是SO,但现在我已经解决了它只改变位置一次而不是之后的问题。 我的代码:
//onCreate()
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
gps = new GPSTracker(this);
public void updateCoordinates(DIRECTIONS direction ) // DIRECTIONS is an enum
{
double tempLatitude, tempLongitude;
tempLatitude = gps.getLatitude();
tempLongitude = gps.getLongitude();
if(direction == DIRECTIONS.UP)
{
tempLatitude = gps.getLatitude() + 1;
tempLongitude = gps.getLongitude();
}
if(direction == DIRECTIONS.DOWN)
{
tempLatitude = gps.getLatitude() - 1;
tempLongitude = gps.getLongitude();
}
if(direction == DIRECTIONS.LEFT)
{
tempLatitude = gps.getLatitude();
tempLongitude = gps.getLongitude() + 1;
}
if(direction == DIRECTIONS.RIGHT)
{
tempLatitude = gps.getLatitude() - 1;
tempLongitude = gps.getLongitude();
}
Location tempLocation = new Location(provider);
tempLocation.setLatitude(tempLatitude);
tempLocation.setLongitude(tempLongitude);
tempLocation.setAccuracy(500);
tempLocation.setAltitude(0D);
tempLocation.setTime(System.currentTimeMillis());
tempLocation.setBearing(0F);
tempLocation.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos());
setCoordinates(tempLocation);
}
enum DIRECTIONS
{
UP,
DOWN,
LEFT,
RIGHT
}
public void getCoordinates(View v)
{
String latitude, longitude;
gps.getLocation();
if(gps.canGetLocation())
{
latitude = String.valueOf(gps.getLatitude());
longitude = String.valueOf(gps.getLongitude());
coordinates.setText(latitude + "," + longitude);
}
else if(!gps.canGetLocation())
{
coordinates.setText("ERROR");
}
}
public void initializeGPS(boolean state) //Used to add and remove provider in onCreate() and onDestroy()
{
int value = setMockLocationSettings();
try{
if(state)
{
mLocationManager.removeTestProvider(provider);
mLocationManager.addTestProvider(provider, false, true, false, false, true, true, false, 0, 5);
mLocationManager.setTestProviderEnabled(provider, true);
mLocationManager.setTestProviderLocation(provider, gps.getLocation());
}
if(!state)
{
mLocationManager.setTestProviderEnabled(LocationManager.GPS_PROVIDER, false);
mLocationManager.clearTestProviderEnabled(LocationManager.GPS_PROVIDER);
mLocationManager.clearTestProviderLocation(LocationManager.GPS_PROVIDER);
mLocationManager.removeTestProvider(LocationManager.GPS_PROVIDER);
}
}
catch (Exception e) {}
finally {
restoreMockLocationSettings(value);
}
}
public void setCoordinates(Location fake_location)
{
int value = setMockLocationSettings();//toggle ALLOW_MOCK_LOCATION on
try {
mLocationManager.setTestProviderLocation(provider, fake_location);
} catch (SecurityException e) {
e.printStackTrace();
} finally {
restoreMockLocationSettings(value);//toggle ALLOW_MOCK_LOCATION off
}
}
private int setMockLocationSettings() {
int value = 1;
try {
value = Settings.Secure.getInt(getContentResolver(),
Settings.Secure.ALLOW_MOCK_LOCATION);
Settings.Secure.putInt(getContentResolver(),
Settings.Secure.ALLOW_MOCK_LOCATION, 1);
} catch (Exception e) {
e.printStackTrace();
}
return value;
}
private void restoreMockLocationSettings(int restore_value) {
try {
Settings.Secure.putInt(getContentResolver(),
Settings.Secure.ALLOW_MOCK_LOCATION, restore_value);
} catch (Exception e) {
e.printStackTrace();
}
}
要获得GPS坐标,我使用名为GPSTracker的类
import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
public class GPSTracker extends Service implements LocationListener {
private final Context mContext;
// flag for GPS status
boolean isGPSEnabled = false;
// flag for network status
boolean isNetworkEnabled = false;
// flag for GPS status
boolean canGetLocation = false;
Location location; // location
double latitude; // latitude
double longitude; // longitude
// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
// Declaring a Location Manager
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} else {
this.canGetLocation = true;
// First get location from Network Provider
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
/**
* Stop using GPS listener
* Calling this function will stop using GPS in your app
* */
public void stopUsingGPS(){
if(locationManager != null){
locationManager.removeUpdates(GPSTracker.this);
}
}
/**
* Function to get latitude
* */
public double getLatitude(){
if(location != null){
latitude = location.getLatitude();
}
// return latitude
return latitude;
}
/**
* Function to get longitude
* */
public double getLongitude(){
if(location != null){
longitude = location.getLongitude();
}
// return longitude
return longitude;
}
/**
* Function to check GPS/wifi enabled
* @return boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}
/**
* Function to show settings alert dialog
* On pressing Settings button will lauch Settings Options
* */
public void showSettingsAlert(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
// Setting Dialog Title
alertDialog.setTitle("GPS is settings");
// Setting Dialog Message
alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");
// On pressing Settings button
alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});
// on pressing cancel button
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
@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;
}
}
我猜问题是我设置的提供程序,因为在我添加initializeGPS()
之前它曾经给出了很多错误