gpt4 book ai didi

android - 用于测试地理围栏应用程序的模拟位置

转载 作者:行者123 更新时间:2023-11-29 17:49:43 25 4
gpt4 key购买 nike

我正在开发一个非常基本的应用程序,我正在创建一个 5000 米半径的地理围栏,以我的位置为中心。

    public class MainActivity extends ActionBarActivity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, LocationListener,
com.google.android.gms.location.LocationListener,
LocationClient.OnAddGeofencesResultListener {

private LocationClient mLocationClient;
private LocationRequest mLocationRequest;
// Milliseconds per second
private static final int MILLISECONDS_PER_SECOND = 1000;
// Update frequency in seconds
public static final int UPDATE_INTERVAL_IN_SECONDS = 5;
// Update frequency in milliseconds
private static final long UPDATE_INTERVAL = MILLISECONDS_PER_SECOND
* UPDATE_INTERVAL_IN_SECONDS;
// The fastest update frequency, in seconds
private static final int FASTEST_INTERVAL_IN_SECONDS = 1;
// A fast frequency ceiling in milliseconds
private static final long FASTEST_INTERVAL = MILLISECONDS_PER_SECOND
* FASTEST_INTERVAL_IN_SECONDS;

boolean mUpdatesRequested;
private Editor mEditor;
private SharedPreferences mPrefs;
private List<Geofence> mGeofenceLists = new ArrayList<Geofence>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_item);

mUpdatesRequested = false;
mPrefs = getPreferences(MODE_PRIVATE);
mEditor = mPrefs.edit();

// Create the LocationRequest object
mLocationRequest = LocationRequest.create();
// Use high accuracy
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// Set the update interval to 5 seconds
mLocationRequest.setInterval(UPDATE_INTERVAL);
// Set the fastest update interval to 1 second
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);

// Location Client Object
mLocationClient = new LocationClient(this, this, this);

}

@Override
protected void onStart() {
super.onStart();
// Connect the client.
mLocationClient.connect();
}

@Override
protected void onStop() {
// Disconnecting the client invalidates it.
mLocationClient.disconnect();
super.onStop();
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
// Save the current setting for updates
mEditor.putBoolean("KEY_UPDATES_ON", mUpdatesRequested);
mEditor.commit();
super.onPause();
}

@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();

/*
* Get any previous setting for location updates Gets "false" if an
* error occurs
*/
if (mPrefs.contains("KEY_UPDATES_ON")) {
mUpdatesRequested = mPrefs.getBoolean("KEY_UPDATES_ON", false);

// Otherwise, turn off location updates
} else {
mEditor.putBoolean("KEY_UPDATES_ON", false);
mEditor.commit();
}
}

@Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
System.out.println("DISCONNECTED!!!!!!!!!!!!!!!!!!");
}

@Override
public void onConnected(Bundle connectionHint) {
// TODO Auto-generated method stub

// Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();

// Toast.makeText(this,"lat: "+location.getLatitude()+" long: "+location.getLongitude(),
// Toast.LENGTH_LONG).show();
/*
* try { getAddress(); } catch (IOException e) { // TODO Auto-generated
* catch block e.printStackTrace(); }
*/
// If already requested, start periodic updates

// Location location = mLocationClient.getLastLocation();

Geofence geofence = new Geofence.Builder().setRequestId("geo1")
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
.setCircularRegion(89.535354, -88.575510, 5000)
.setExpirationDuration(Geofence.NEVER_EXPIRE).build();

mGeofenceLists.add(geofence);
mLocationClient.addGeofences(mGeofenceLists,
getTransitionPendingIntent(), this);

if (true) { // Need to update when user is asking for updates with
// mUpdatesRequested
mLocationClient.requestLocationUpdates(mLocationRequest, this);

}

}

private PendingIntent getTransitionPendingIntent() {
// Create an explicit Intent
PendingIntent geoFencePendingIntent = PendingIntent.getService(this, 0,
new Intent(this, TransitionsIntentService.class),
PendingIntent.FLAG_UPDATE_CURRENT);
return geoFencePendingIntent;

}

/*
* private void getAddress() throws IOException { // TODO Auto-generated
* method stub
*
* double latitude = mLocationClient.getLastLocation().getLatitude(); double
* longitude = mLocationClient.getLastLocation().getLongitude(); Geocoder
* geocoder; List<Address> addresses; geocoder = new Geocoder(this,
* Locale.getDefault()); addresses = geocoder.getFromLocation(latitude,
* longitude, 1);
*
* String address = addresses.get(0).getAddressLine(0); String city =
* addresses.get(0).getAddressLine(1); String country =
* addresses.get(0).getAddressLine(2);
*
*
* Toast.makeText(this,"You are at : "+address+"\n"+city+" ,"+country,
* Toast.LENGTH_SHORT).show();
*
*
* }
*/

@Override
public void onDisconnected() {
// TODO Auto-generated method stub

}

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub

// System.out.println("LOCATION CHANGE TRIGGERED!!!!!!!!!!!!!!!");
/*
* String msg = "Updated Location: " +
* Double.toString(location.getLatitude()) + "," +
* Double.toString(location.getLongitude()); Toast.makeText(this, msg,
* Toast.LENGTH_SHORT).show();
*/

}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

public class GeofenceSampleReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

String action = intent.getAction();

if (TextUtils.equals(action,
"com.example.android.geofence.ACTION_GEOFENCE_TRANSITION")) {

/* handleGeofenceTransition(context, intent); */

System.out
.println("!!!!!!!!!!!!!!!!!!GEOFENCE in sample receiverTRIGGERED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");

}
}

}

public class TransitionsIntentService extends IntentService {
public static final String TRANSITION_INTENT_SERVICE = "ReceiveTransitionsIntentService";

public TransitionsIntentService() {
super(TRANSITION_INTENT_SERVICE);
}

@Override
protected void onHandleIntent(Intent arg0) {
// TODO Auto-generated method stub



System.out
.println("!!!!!!!!!!!!!!!!!!GEOFENCE in handle intent TRIGGERED!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");


}
}

@Override
public void onAddGeofencesResult(int statusCode, String[] geofenceRequestIds) {
// TODO Auto-generated method stub

}

我没有在代码中得到任何触发。是因为我已经在地理围栏内了吗???如何使用我创建的地理围栏测试入口点和导出点。

最佳答案

我自己尝试使用 MockLocations 来触发围栏,但我无法让它发挥作用。你可以试试这个:

  1. 转到设置、开发人员选项并允许 mockLocations
  2. 然后只需下载一个应用程序来更改您的 GPS 位置,例如 fakegps 或类似的东西。

这种方式适合我。

关于android - 用于测试地理围栏应用程序的模拟位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23816993/

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