mrmans0n / smart-location-lib

Android library project that lets you manage the location updates to be as painless as possible
1.65k stars 353 forks source link

Capture the Geofence transitions #148

Open sokhornhoun opened 8 years ago

sokhornhoun commented 8 years ago

How to capture the Geofence transitions without the app running by intent service. you can just tell how to hook up to intent service. please give example.

thanks,

mssaravanan commented 8 years ago

Pls give example code for receive the geofence from background ..

I have register the brodcast receiver in manifest ...

`

`

And my broadcast receiver is , `public class GeoFenceReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (BROADCAST_INTENT_ACTION.equals(intent.getAction())) { final int transitionType = intent.getIntExtra(TRANSITION_EXTRA_ID, -1); final List geofencingIds = intent.getStringArrayListExtra(GEOFENCES_EXTRA_ID); Location location = intent.getParcelableExtra(LOCATION_EXTRA_ID); Toast.makeText(context, getTransitionNameFromType(transitionType), Toast.LENGTH_SHORT).show(); sendNotification(getTransitionNameFromType(transitionType),context); for (final String geofenceId : geofencingIds) {

        }
    }

}

private String getTransitionNameFromType(int transitionType) {
    switch (transitionType) {
        case Geofence.GEOFENCE_TRANSITION_ENTER:
            return "enter";
        case Geofence.GEOFENCE_TRANSITION_EXIT:
            return "exit";
        default:
            return "dwell";
    }
}

/**
 * Posts a notification in the notification bar when a transition is detected.
 * If the user clicks the notification, control goes to the MainActivity.
 */
private void sendNotification(String notificationDetails,Context context) {
    // Create an explicit content Intent that starts the main Activity.
    Intent notificationIntent = new Intent(context, MainActivity.class);

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

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

    // Define the notification settings.
    builder.setSmallIcon(R.drawable.ic_menu_share)
            // In a real app, you may want to use a library like Volley
            // to decode the Bitmap.
            .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
                    R.drawable.ic_menu_slideshow))
            .setColor(Color.RED)
            .setContentTitle("iAttention")
            .setContentText(notificationDetails)
            .setContentIntent(notificationPendingIntent);

    // Dismiss notification once the user touches it.
    builder.setAutoCancel(true);

    // Get an instance of the Notification manager
    NotificationManager mNotificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    // Issue the notification
    mNotificationManager.notify(0, builder.build());
}

}`