我已经为GeoFencing创建了注册类,并创建了一个Activity Main来使用它。我使用了此页面上的代码:[Android LocationServices.GeofencingApi example usage 这是我的活动主页:
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.util.ArrayList;
import java.util.List;
public class MapsActivity extends FragmentActivity implements GeofencingRegistererCallbacks{
private static final String TAG= "GeoFencingTEst-MainActivity";
private GoogleMap mMap; // Might be null if Google Play services APK is not available.
private GeofencingRegisterer gRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setUpMapIfNeeded();
gRegister= new GeofencingRegisterer(this);
gRegister.setGeofencingCallback(this);
List<Geofence> geofenceList= new ArrayList<Geofence>();
Geofence geofence=new Geofence.Builder()
.setRequestId("1")
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER)
.setCircularRegion(37.0, -122.0, 10)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build();
geofenceList.add(geofence);
gRegister.registerGeofences(geofenceList);
//startService(new Intent(this, GeofencingReceiver.class));
Log.e(TAG, ""+gRegister.getRequestPendingIntent());
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
/**
* Sets up the map if it is possible to do so (i.e., the Google Play services APK is correctly
* installed) and the map has not already been instantiated.. This will ensure that we only ever
* call {@link #setUpMap()} once when {@link #mMap} is not null.
* <p/>
* If it isn't installed {@link SupportMapFragment} (and
* {@link com.google.android.gms.maps.MapView MapView}) will show a prompt for the user to
* install/update the Google Play services APK on their device.
* <p/>
* A user can return to this FragmentActivity after following the prompt and correctly
* installing/updating/enabling the Google Play services. Since the FragmentActivity may not
* have been completely destroyed during this process (it is likely that it would only be
* stopped or paused), {@link #onCreate(Bundle)} may not be called again so we should call this
* method in {@link #onResume()} to guarantee that it will be called.
*/
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
/**
* This is where we can add markers or lines, add listeners or move the camera. In this case, we
* just add a marker near Africa.
* <p/>
* This should only be called once and when we are sure that {@link #mMap} is not null.
*/
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
@Override
public void onApiClientConnected() {
Log.e(TAG,"onApiClientConnected");
}
@Override
public void onApiClientSuspended() {
Log.e(TAG,"onApiClientSuspended");
}
@Override
public void onApiClientConnectionFailed(ConnectionResult connectionResult) {
Log.e(TAG,"onApiClientConnectionFailed");
}
@Override
public void onGeofencesRegisteredSuccessful() {
Log.e(TAG,"onGeofencesRegisteredSuccessful"+gRegister.getRequestPendingIntent());
}
}
当我使用模拟器进入感兴趣的区域时,我收到通知时遇到问题。 Geofencing注册成功发生,如logcat所示:
`
01-10 10:57:46.075: E/it.unical.dimes.tesi.geofencingtest.GeofencingRegisterer(2295): onConnected
01-10 10:57:46.075: E/GeoFencingTEst-MainActivity(2295): onApiClientConnected
01-10 10:57:46.075: E/it.unical.dimes.tesi.geofencingtest.GeofencingRegisterer(2295): Call diverso da null
01-10 10:57:46.075: E/it.unical.dimes.tesi.geofencingtest.GeofencingRegisterer(2295): new pending intent
01-10 10:57:46.077: E/it.unical.dimes.tesi.geofencingtest.GeofencingRegisterer(2295): AddGeofences
01-10 10:57:46.146: D/WifiService(1224): acquireWifiLockLocked: WifiLock{NlpWifiLock type=2 binder=android.os.BinderProxy@38d081bb}
01-10 10:57:46.201: I/ActivityManager(1224): Displayed it.unical.dimes.tesi.geofencingtest/.MapsActivity: +1s4ms
01-10 10:57:46.207: E/it.unical.dimes.tesi.geofencingtest.GeofencingRegisterer(2295): Status:Status{statusCode=SUCCESS, resolution=null}
01-10 10:57:46.207: E/it.unical.dimes.tesi.geofencingtest.GeofencingRegisterer(2295): Status: Successful callback
01-10 10:57:46.207: E/it.unical.dimes.tesi.geofencingtest.GeofencingRegisterer(2295): get request pending intent
01-10 10:57:46.207: E/it.unical.dimes.tesi.geofencingtest.GeofencingRegisterer(2295): pending intent not null
01-10 10:57:46.207: E/GeoFencingTEst-MainActivity(2295): onGeofencesRegisteredSuccessfulPendingIntent{1cc51dc6: android.os.BinderProxy@28ee5bfd}`
我认为问题是服务器不在后台运行,因为这是日志:
01-10 10:57:56.406: I/CheckinRequestBuilder(1708): Classify the device as Phone.
01-10 10:57:59.727: I/ConfigFetchService(1708): fetch service done; releasing wakelock
01-10 10:57:59.729: I/ActivityManager(1224): Waited long enough for: ServiceRecord{14d38e65 u0 com.google.android.gms/.wearable.service.WearableService}
01-10 10:57:59.730: I/ConfigFetchService(1708): stopping self
01-10 10:57:59.733: I/ConfigService(1656): onDestroy
有没有人可以使用解决方案或示例代码?
答案 0 :(得分:0)
您是否有服务来处理因进入地理围栏而被解雇的意图?