我正在开发一个非常基本的应用程序,我正在创建一个半径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
}
}
我没有在代码中得到任何触发。是因为我已经在地理围栏中了??? 如何使用我创建的地理围栏来测试进入和退出点。
答案 0 :(得分:3)
我试图自己使用MockLocations
来触发围栏,但我无法使其发挥作用。
你可以试试这个:
mockLocations
。 这样对我有用。