My GeofenceTransitionsIntentService does not send any notifications when a geocenter transition occurs

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" /> <!-- activities --> <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; } // Get the transition type. int geofenceTransition = geofencingEvent.getGeofenceTransition(); // Test that the reported transition was of interest. if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER || geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) { // Get the geofences that were triggered. A single event can trigger multiple geofences. List<Geofence> triggeringGeofences = geofencingEvent.getTriggeringGeofences(); // Get the transition details as a String. String geofenceTransitionDetails = getGeofenceTransitionDetails( this, geofenceTransition, triggeringGeofences ); Log.e(TAG, geofenceTransitionDetails); // Send notification and log the transition details. sendNotification(geofenceTransitionDetails); Log.i(TAG, geofenceTransitionDetails); } else { Log.e(TAG, "geofence transition invalid type"); } } private String getGeofenceTransitionDetails( Context context, int geofenceTransition, List<Geofence> triggeringGeofences) { String geofenceTransitionString = getTransitionString(geofenceTransition); // Get the Ids of each geofence that was triggered. ArrayList triggeringGeofencesIdsList = new ArrayList(); for (Geofence geofence : triggeringGeofences) { triggeringGeofencesIdsList.add(geofence.getRequestId()); } String triggeringGeofencesIdsString = TextUtils.join(", ", triggeringGeofencesIdsList); return geofenceTransitionString + ": " + triggeringGeofencesIdsString; } private void sendNotification(String notificationDetails) { // Create an explicit content Intent that starts the main Activity. Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class); // Construct a task stack. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); // Add the main Activity to the task stack as the parent. stackBuilder.addParentStack(MainActivity.class); // Push the content Intent onto the stack. stackBuilder.addNextIntent(notificationIntent); // Get a PendingIntent containing the entire back stack. PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); // Get a notification builder that compatible with platform versions >= 4 NotificationCompat.Builder builder = new NotificationCompat.Builder(this); // Define the notification settings. builder.setContentTitle(notificationDetails) .setContentText("Click notification to return to app") .setContentIntent(notificationPendingIntent); // Dismiss notification once the user touches it. builder.setAutoCancel(true); // Get an instance of the Notification manager NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // Issue the notification mNotificationManager.notify(0, builder.build()); } private String getTransitionString(int transitionType) { switch (transitionType) { case Geofence.GEOFENCE_TRANSITION_ENTER: return "transition - enter"; case Geofence.GEOFENCE_TRANSITION_EXIT: return "transition - exit"; default: return "unknown transition"; } } } 

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"); } } } 
+5
source share

All Articles