3

I have run a sample of Geofence and I have assigned two set of Lat , Lng and radius and run the sample. When I tap over register geofence I am getting following things in my log

Location Services is available
Client connected
Add Geofences: Success GeofenceRequestIds=[1, 2]
com.example.android.geofence.ACTION_GEOFENCES_ADDED

but nothing happens after that, I don't know what the issue I am passed the current location by enabling gps but no effect.

I don't know when I am gonna get the notification when a fence is crossed, to test it I have passed the same geo coordinates that are in fencing but still no alerts I am getting.

I have tried even replacing getService() by getBroadcast() inside createRequestPendingIntent function after reading few threads over stackoverflow , but no luck.

I have also tried it with

**Geofence #1 and Geofence #2**

Lat : 23.039568000000003
Lng : 72.56600400000002
Radius :1

and I have also set my current position in genymotion to above co ordinates to make it work but no notification.

Even on a real device my 3G is enable and I am moving with the device but no change so far.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Hunt
  • 8,215
  • 28
  • 116
  • 256

2 Answers2

2

you are set radius too small google-play service not give notification for 1 meter when you are increase radius its give better result for that and try it for geo-fencing.

in GeofenceReceiver

Intent broadcastIntent = new Intent();
@Override
public void onReceive(Context context, Intent intent) {
    this.context = context;

    broadcastIntent.addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES);

    if (LocationClient.hasError(intent)) {
        handleError(intent);
    } else {
        handleEnterExit(intent);
    }
}

private void handleError(Intent intent) {
    // Get the error code
    int errorCode = LocationClient.getErrorCode(intent);

    // Get the error message
    String errorMessage = LocationServiceErrorMessages.getErrorString(
            context, errorCode);

    // Log the error
    Log.e(GeofenceUtils.APPTAG, context.getString(
            R.string.geofence_transition_error_detail, errorMessage));

    // Set the action and error message for the broadcast intent
    broadcastIntent.setAction(GeofenceUtils.ACTION_GEOFENCE_ERROR)
            .putExtra(GeofenceUtils.EXTRA_GEOFENCE_STATUS, errorMessage);

    // Broadcast the error *locally* to other components in this app
    LocalBroadcastManager.getInstance(context).sendBroadcast(
            broadcastIntent);
}

private void handleEnterExit(Intent intent) {
    // Get the type of transition (entry or exit)
    int transition = LocationClient.getGeofenceTransition(intent);

    // Test that a valid transition was reported
    if ((transition == Geofence.GEOFENCE_TRANSITION_ENTER)
            || (transition == Geofence.GEOFENCE_TRANSITION_EXIT)) {

        // Post a notification
        List<Geofence> geofences = LocationClient
                .getTriggeringGeofences(intent);
        String[] geofenceIds = new String[geofences.size()];
        String ids = TextUtils.join(GeofenceUtils.GEOFENCE_ID_DELIMITER,
                geofenceIds);
        String transitionType = getTransitionString(transition);
        sendNotification(transitionType, ids);

        for (int index = 0; index < geofences.size(); index++) {
            Geofence geofence = geofences.get(index);

        }
        // Create an Intent to broadcast to the app
        broadcastIntent
                .setAction(GeofenceUtils.ACTION_GEOFENCE_TRANSITION)
                .addCategory(GeofenceUtils.CATEGORY_LOCATION_SERVICES);

        LocalBroadcastManager.getInstance(sqlitewraper.context)
                .sendBroadcast(broadcastIntent);

        // Log the transition type and a message
        Log.d(GeofenceUtils.APPTAG, transitionType + ": " + ids);
        Log.d(GeofenceUtils.APPTAG, context
                .getString(R.string.geofence_transition_notification_text));

        // In debug mode, log the result
        Log.d(GeofenceUtils.APPTAG, "transition");

        // An invalid transition was reported
    } else {
        // Always log as an error
        Log.e(GeofenceUtils.APPTAG, context.getString(
                R.string.geofence_transition_invalid_type, transition));
    }
}

/**
 * Posts a notification in the notification bar when a transition is
 * detected. If the user clicks the notification, control goes to the main
 * Activity.
 * 
 * @param transitionType
 *            The type of transition that occurred.
 * 
 */

private void sendNotification(String transitionType, String locationName) {

    // Create an explicit content Intent that starts the main Activity
    Intent notificationIntent = new Intent(sqlitewraper.context,
            MainActivity.class);

    // Construct a task stack
    TaskStackBuilder stackBuilder = TaskStackBuilder
            .create(sqlitewraper.context);

    // Adds 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's compatible with platform versions
    // >= 4
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            sqlitewraper.context);

    // Set the notification contents
    builder.setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(transitionType + ": " + locationName)
            .setContentText(
                    sqlitewraper.context
                            .getString(R.string.geofence_transition_notification_text))
            .setContentIntent(notificationPendingIntent);

    // Get an instance of the Notification manager
    NotificationManager mNotificationManager = (NotificationManager) sqlitewraper.context
            .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 ("enter geofence");

            case Geofence.GEOFENCE_TRANSITION_EXIT:
                return ("exit from  geofence");

            default:

        return ("Transisation unknown");
        }
 }

Change in sample code GeofenceRequester

    Intent intent = new Intent("packagename.ACTION_RECEIVE_GEOFENCE");

//  Intent intent = new Intent(mActivity, ReceiveTransitionsIntentService.class);


return PendingIntent.getBroadcast(

                   sqlitewraper.context,
                    0,
                   intent,
                    PendingIntent.FLAG_UPDATE_CURRENT);
        }

change in Manifest file

<receiver android:name="packagename.GeofenceReceiver"
            android:exported="false" >
            <intent-filter>
                <action android:name="packagename.ACTION_RECEIVE_GEOFENCE" />
              <category android:name="packagename" />
            </intent-filter>
        </receiver>
MIkka Marmik
  • 1,101
  • 11
  • 29
  • then what should be the acceptable radius according to you , as i have tried with your code and a radius of 5 but still m not getting any update , there are two set of geofencing co-ordinates and should i set it for `Geofence.GEOFENCE_TRANSITION_ENTER` for both sets – Hunt Jul 05 '14 at 12:52
  • try to radius minimum 100m then try it hope so its working fine. geo-fencing takes radius in meter you are set radius 5 means its take 5 meter so it is too small take 100 it surely works for you hpe so its working fine. – MIkka Marmik Jul 06 '14 at 11:25
0

Hey once check whether access to my location is selected. Ie in setting -> Location access

Abhimaan
  • 1,893
  • 2
  • 19
  • 26