-1

MainActivity.java

    import android.Manifest;
    import android.app.PendingIntent;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.location.Location;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.support.v4.app.ActivityCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.util.Log;

    import com.google.android.gms.common.ConnectionResult;
    import com.google.android.gms.common.GooglePlayServicesUtil;
    import com.google.android.gms.common.api.GoogleApiClient;
    import com.google.android.gms.common.api.ResultCallback;
    import com.google.android.gms.common.api.Status;
    import com.google.android.gms.location.Geofence;
    import com.google.android.gms.location.GeofencingRequest;
    import com.google.android.gms.location.LocationListener;
    import com.google.android.gms.location.LocationRequest;
    import com.google.android.gms.location.LocationServices;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.UUID;

    public class MainActivity extends AppCompatActivity implements LocationListener {

        PendingIntent mGeofencePendingIntent;
        public static final int CONNECTION_FAILURE_RESOLUTION_REQUEST = 100;
        private List<Geofence> mGeofenceList;
        private GoogleApiClient mGoogleApiClient;
        public static final String TAG = "Activity";
        LocationRequest mLocationRequest;
        double currentLatitude =12.9141 , currentLongitude = 77.6233;
        Boolean locationFound;
        protected LocationManager locationManager;
        protected LocationListener locationListener;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            if (savedInstanceState == null) {

                mGeofenceList = new ArrayList<Geofence>();

                int resp = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
                if (resp == ConnectionResult.SUCCESS) {

                    initGoogleAPIClient();

                    createGeofences(currentLatitude, currentLongitude);

                } else {
                    Log.e(TAG, "Your Device doesn't support Google Play Services.");
                }

                // Create the LocationRequest object
                mLocationRequest = LocationRequest.create()
                        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                        .setInterval(1 * 1000)        // 10 seconds, in milliseconds
                        .setFastestInterval(1 * 1000); // 1 second, in milliseconds

            }

        }

        public void initGoogleAPIClient() {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(connectionAddListener)
                    .addOnConnectionFailedListener(connectionFailedListener)
                    .build();
            mGoogleApiClient.connect();
        }

        private GoogleApiClient.ConnectionCallbacks connectionAddListener =
                new GoogleApiClient.ConnectionCallbacks() {
                    @Override
                    public void onConnected(Bundle bundle) {
                        Log.i(TAG, "onConnected");

                        Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

                        if (location == null) {
                            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, MainActivity.this);

                        } else {
                            //If everything went fine lets get latitude and longitude
                            currentLatitude = location.getLatitude();
                            currentLongitude = location.getLongitude();

                            Log.i(TAG, currentLatitude + " WORKS " + currentLongitude);

                            //createGeofences(currentLatitude, currentLongitude);
                            //registerGeofences(mGeofenceList);
                        }

                        try{
                            LocationServices.GeofencingApi.addGeofences(
                                    mGoogleApiClient,
                                    getGeofencingRequest(),
                                    getGeofencePendingIntent()
                            ).setResultCallback(new ResultCallback<Status>() {

                                @Override
                                public void onResult(Status status) {
                                    if (status.isSuccess()) {
                                        Log.i(TAG, "Saving Geofence");

                                    } else {
                                        Log.e(TAG, "Registering geofence failed: " + status.getStatusMessage() +
                                                " : " + status.getStatusCode());
                                    }
                                }
                            });

                        } catch (SecurityException securityException) {
                            // Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission.
                            Log.e(TAG, "Error");
                        }
                    }

                    @Override
                    public void onConnectionSuspended(int i) {

                        Log.e(TAG, "onConnectionSuspended");

                    }
                };

        private GoogleApiClient.OnConnectionFailedListener connectionFailedListener =
                new GoogleApiClient.OnConnectionFailedListener() {
                    @Override
                    public void onConnectionFailed(ConnectionResult connectionResult) {
                        Log.e(TAG, "onConnectionFailed");
                    }
                };

        /**
         * Create a Geofence list
         */
        public void createGeofences(double latitude, double longitude) {
            String id = UUID.randomUUID().toString();
            Geofence fence = new Geofence.Builder()
                    .setRequestId(id)
                    .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
                    .setCircularRegion(latitude, longitude, 200)
                    .setExpirationDuration(Geofence.NEVER_EXPIRE)
                    .build();
            mGeofenceList.add(fence);
        }

        private GeofencingRequest getGeofencingRequest() {
            GeofencingRequest.Builder builder = new GeofencingRequest.Builder();
            builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER);
            builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_DWELL);
            builder.addGeofences(mGeofenceList);
            return builder.build();
        }

        private PendingIntent getGeofencePendingIntent() {
            // Reuse the PendingIntent if we already have it.
            if (mGeofencePendingIntent != null) {
                return mGeofencePendingIntent;
            }
            Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
            // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when
            // calling addGeofences() and removeGeofences().
            return PendingIntent.getService(this, 0, intent, PendingIntent.
                    FLAG_UPDATE_CURRENT);
        }

        @Override
        public void onLocationChanged(Location location) {
            currentLatitude = location.getLatitude();
            currentLongitude = location.getLongitude();
            Log.i(TAG, "onLocationChanged");
        }

    }

GeoTransitionsIntentservices.java

    import android.app.IntentService;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Build;
    import android.support.v4.app.NotificationCompat;
    import android.util.Log;

    import com.google.android.gms.location.Geofence;
    import com.google.android.gms.location.GeofencingEvent;

    public class GeofenceTransitionsIntentService extends IntentService {

        private static final String TAG = "GeofenceTransitions";

        public GeofenceTransitionsIntentService() {
            super("GeofenceTransitionsIntentService");
        }

        @Override
        protected void onHandleIntent(Intent intent) {

            Log.i(TAG, "onHandleIntent");

            GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
            if (geofencingEvent.hasError()) {
                //String errorMessage = GeofenceErrorMessages.getErrorString(this,
                //      geofencingEvent.getErrorCode());
                Log.e(TAG, "Goefencing Error " + geofencingEvent.getErrorCode());
                return;
            }

            // Get the transition type.
            int geofenceTransition = geofencingEvent.getGeofenceTransition();

            Log.i(TAG, "geofenceTransition = " + geofenceTransition + " Enter : " + Geofence.GEOFENCE_TRANSITION_ENTER + "Exit : " + Geofence.GEOFENCE_TRANSITION_EXIT);
            if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER || geofenceTransition == Geofence.GEOFENCE_TRANSITION_DWELL){

                showNotification("Entered the location", "Entered the Location");
            }
            else if(geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
                Log.i(TAG, "Showing Notification...");

                showNotification("Exited", "Exited the Location");
            } else {
                // Log the error.
                showNotification("Error", "Error");
                Log.e(TAG, "Error ");
            }
        }

        public void showNotification(String text, String bigText) {

            // 1. Create a NotificationManager
            NotificationManager notificationManager =
                    (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

            // 2. Create a PendingIntent for AllGeofencesActivity
            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            // 3. Create and send a notification
            Notification notification = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)

                    .setContentTitle("Geofence Monitor")

                    .setContentText(text)
                    .setContentIntent(pendingNotificationIntent)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(bigText))
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setAutoCancel(true)
                    .build();
            notificationManager.notify(0, notification);

            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher);
            } else {
                new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher);
            }

        }
    }

I want to implement Dwell when user exits the region. I have developed the geofence notification when user exits and entry the region and now I want to implement it has to monitor for a certain period of time and after exiting.

It should make toast message and if I implement I am getting error message.

halfer
  • 18,701
  • 13
  • 79
  • 158

1 Answers1

0

In your IntentService use toaster like this, it will not give error

 private void sendNotification(String notificationDetails) {

        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), "here is your toast msg", Toast.LENGTH_LONG).show();
            }
        });
        // 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's compatible with platform versions >= 4
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

        // Define the notification settings.
        builder.setSmallIcon(R.drawable.ic_launcher)
                // In a real app, you may want to use a library like Volley
                // to decode the Bitmap.
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                        R.drawable.ic_launcher))
                .setColor(Color.RED)
                .setContentTitle(notificationDetails)
                .setContentText(getString(R.string.geofence_transition_notification_text))
                .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());
    } 

And in

      @Override
    protected void onHandleIntent(Intent intent) {
 // Get the transition type.
        int geofenceTransition = geofencingEvent.getGeofenceTransition();

        // Test that the reported transition was of interest.
        if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT){
             sendNotification("your notification details");
             sendAPICall("some token","body","my call");
        } 
}

//// Api call

public void sendAPICall(final String reg_token, final  String body, final String title) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                try {
                    OkHttpClient client = new OkHttpClient();
                    JSONObject json = new JSONObject();
                    JSONObject dataJson = new JSONObject();
                    dataJson.put("body", body);
                    dataJson.put("title", title);
                    json.put("notification", dataJson);
                    json.put("to", reg_token);
                    RequestBody body = RequestBody.create(JSON, json.toString());
                    Request request = new Request.Builder()
                            .header("Authorization", "key=" + Constants.GCM_AUTH_KEY)
                            .url("http://learnologic.com/send")
                            .post(body)
                            .build();
                    okhttp3.Response response = client.newCall(request).execute();
                    String finalResponse = response.body().string();
                    Logger.showDebugLog(finalResponse);
                } catch (Exception e) {
                    Logger.showErrorLog(e.toString());
                }
                return null;
            }
        }.execute();

    }

// stop monitor geofence

public void stopMonitoringGeofences() {
        if (!mGoogleApiClient.isConnected()) {
            Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            // Remove geofences.
            LocationServices.GeofencingApi.removeGeofences(
                    mGoogleApiClient,
                    // This is the same pending intent that was used in addGeofences().
                    getGeofencePendingIntent()
            ).setResultCallback(this); // Result processed in onResult().
        } catch (SecurityException securityException) {
            // Catch exception generated if the app does not use ACCESS_FINE_LOCATION permission.
            logSecurityException(securityException);
        }
    }

 private PendingIntent getGeofencePendingIntent() {
        // Reuse the PendingIntent if we already have it.
        if (mGeofencePendingIntent != null) {
            return mGeofencePendingIntent;
        }
        Intent intent = new Intent(this, GeofenceTransitionsIntentService.class);
        // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
        // addGeofences() and removeGeofences().
        return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    }
Shaz Hemani
  • 419
  • 2
  • 10
  • It is not working for me i have a already getting notification when i enter/exit the geofence my query is when user exits the region it should make toast/notification message – Barath Kumar RA Feb 28 '17 at 11:02
  • And I also have another query is there any possibility of getting notification in between entry and exit if the user retains in particular region for a certain period of time we should send notification to the user something like greeting message to the user by setting timing duration? – Barath Kumar RA Feb 28 '17 at 12:38
  • oops, that was in my previous answer, any way you can explore `new Geofence.Builder().setLoiteringDelay(1000*60)` method while adding geo points. And kindly rate my post if my solution worked for you – Shaz Hemani Feb 28 '17 at 13:04
  • Thanks sir it is working for me (Loitering delay) and is there any possibility for making a API Call when users enter/exits the region each time if there any possibility please help me and thanks for help. – Barath Kumar RA Feb 28 '17 at 14:32
  • My pleasure, yes you can invoke api call from your Intent Service, Which Library are you using for api calls in your app? i will reply the solution accordingly. for the time been i am writing a small piece of code for invoking a simple api call. kindly check my reply above – Shaz Hemani Mar 01 '17 at 05:27
  • Thanks for the reply and where should i place this class whether in mainactivity or geofencetransistionsintentservices class and is there and code for stopmonitoring the geofence if the user is already registered. – Barath Kumar RA Mar 01 '17 at 11:03
  • in geofencetransistionsintentservices, didn't get your other point – Shaz Hemani Mar 01 '17 at 11:05
  • ya it's working sir and what about stop monitoring geofences? – Barath Kumar RA Mar 01 '17 at 11:14
  • Sir how to implement stop monitoring the geofence if the user is already registered sir please reply as soon as possible – Barath Kumar RA Mar 02 '17 at 06:15
  • Alright I am adding code in answer, kindly vote my answer as well – Shaz Hemani Mar 02 '17 at 06:26
  • OK sir definitely – Barath Kumar RA Mar 02 '17 at 06:27
  • Sir can i implement the API call when use enters/exits the geofence please help me sir . – Barath Kumar RA Mar 13 '17 at 11:05
  • sir i want implement the API call using http url connection simply without json or gson please help me sir very urgent please help me sir – Barath Kumar RA Mar 15 '17 at 05:37