I am trying to implement the function of adding and monitoring geopotentials, and I thought that this tutorial is the best one to start with.
I did everything as they described, and it works, but not in the way I would like. I noticed that geofence transistors are not being tracked, what I really want to say is that when I enter geofence, nothing happens, but when I run the application inside the geofence, I get some response. I also noticed that none of the methods from my GeofenceTransitionsIntentService are executed, which actually means that geospatial transition data will never be broadcast to MainActivity.
Honestly, this is the first time I'm trying to implement such a function, so I really don't know what could be causing this problem. If you have an idea, answer, I will be grateful.
This is what I have done so far.
My AndroidManifest
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="at.at.tuwien.hci.hciss2015"> <uses-feature android:glEsVersion="0x00020000" android:required="true" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> ... <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <service android:name="at.at.tuwien.hci.hciss2015.util.GeofenceTransitionsIntentService" /> </application>
My GeofenceTransitionsIntentService.java
public class GeofenceTransitionsIntentService extends IntentService { protected static final String TAG = GeofenceTransitionsIntentService.class.getSimpleName(); public GeofenceTransitionsIntentService() { super(TAG); } @Override public void onCreate() { super.onCreate(); Log.e(TAG, "init GeofenceTransitionsIntentService"); } @Override protected void onHandleIntent(Intent intent) { GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); if (geofencingEvent.hasError()) { Log.e(TAG, "geofencing event error"); return; }
My mainactivity
public class MainActivity extends FragmentActivity implements ConnectionCallbacks, OnConnectionFailedListener, ResultCallback<Status> { protected GoogleApiClient mGoogleApiClient; protected ArrayList<Geofence> mGeofenceList; private PendingIntent mGeofencePendingIntent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); .... mGeofenceList = new ArrayList<Geofence>(); mGeofencePendingIntent = null; populateGeofenceList(); buildGoogleApiClient(); } protected synchronized void buildGoogleApiClient() { mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build(); } private GeofencingRequest getGeofencingRequest() { GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER); builder.addGeofences(mGeofenceList); return builder.build(); } private PendingIntent getGeofencePendingIntent() { if (mGeofencePendingIntent != null) { return mGeofencePendingIntent; } Intent intent = new Intent(this, GeofenceTransitionsIntentService.class); return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); } public void populateGeofenceList() { mGeofenceList.add(new Geofence.Builder() .setRequestId("1") .setCircularRegion(48.178454, 16.369699, 10) .setExpirationDuration(Geofence.NEVER_EXPIRE) .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER) .build()); mGeofenceList.add(new Geofence.Builder() .setRequestId("2") .setCircularRegion(48.17755, 16.369114, 10) .setExpirationDuration(Geofence.NEVER_EXPIRE) .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER) .build()); } @Override protected void onStart() { super.onStart(); mGoogleApiClient.connect(); } @Override protected void onStop() { super.onStop(); LocationServices.GeofencingApi.removeGeofences( mGoogleApiClient, getGeofencePendingIntent() ).setResultCallback(this); if (mGoogleApiClient.isConnected()) { mGoogleApiClient.disconnect(); } } @Override public void onConnected(Bundle bundle) { Location myLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); initCamera(myLocation); LocationServices.GeofencingApi.addGeofences( mGoogleApiClient, getGeofencingRequest(), getGeofencePendingIntent() ).setResultCallback(this); } @Override public void onConnectionSuspended(int cause) { Log.i(TAG, "Connection suspended"); mGoogleApiClient.connect(); } @Override public void onConnectionFailed(ConnectionResult result) { Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode()); } @Override public void onResult(Status status) { if (status.isSuccess()) { Toast.makeText(this, "status: " + status.getStatus(),Toast.LENGTH_SHORT).show(); } else { Log.e(TAG, "something is wrong"); } } }