Java tutorial
/** * Copyright 2014 Google Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.rozrost.www.smartlocationreminder; import android.app.AlarmManager; import android.app.IntentService; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.graphics.BitmapFactory; import android.graphics.Color; import android.media.MediaPlayer; import android.os.PowerManager; import android.support.v4.app.NotificationCompat; import android.support.v4.app.TaskStackBuilder; import android.text.TextUtils; import android.util.Log; import android.widget.Button; import android.widget.Toast; import com.google.android.gms.location.Geofence; import com.google.android.gms.location.GeofencingEvent; import java.text.ParseException; import java.util.ArrayList; import java.util.List; /** * Listener for geofence transition changes. * * Receives geofence transition events from Location Services in the form of an Intent containing * the transition type and geofence id(s) that triggered the transition. Creates a notification * as the output. */ public class GeofenceTransitionsIntentService extends IntentService { protected static final String TAG = "GeofenceTransitionsIS"; /** * This constructor is required, and calls the super IntentService(String) * constructor with the name for a worker thread. */ public GeofenceTransitionsIntentService() { // Use the TAG to name the worker thread. super(TAG); } @Override public void onCreate() { super.onCreate(); } /** * Handles incoming intents. * @param intent sent by Location Services. This Intent is provided to Location * Services (inside a PendingIntent) when addGeofences() is called. */ @Override protected void onHandleIntent(Intent intent) { GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent); if (geofencingEvent.hasError()) { String errorMessage = GeofenceErrorMessages.getErrorString(this, geofencingEvent.getErrorCode()); Log.e(TAG, errorMessage); return; } // Get the transition type. int geofenceTransition = geofencingEvent.getGeofenceTransition(); // Test that the reported transition was of interest. if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) { // 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); String primaryKey = triggeringGeofences.get(0).getRequestId(); // Send notification and log the transition details. Log.i(TAG, geofenceTransitionDetails); DatabaseHelper mDatabaseHelper = new DatabaseHelper(this); try { if (mDatabaseHelper.CheckInBetween(primaryKey)) { AlarmService(primaryKey); } } catch (ParseException e) { e.printStackTrace(); } } else { // Log the error. Log.e(TAG, getString(geofenceTransition)); } } private void AlarmService(String PrimaryKey) { Intent intent = new Intent("com.rozrost.www.alertscreen.Receiver"); intent.putExtra("PrimaryKey", PrimaryKey); PendingIntent pendingIntent = PendingIntent.getActivity(this, 2, intent, PendingIntent.FLAG_CANCEL_CURRENT); AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, 0, pendingIntent); } /** * Gets transition details and returns them as a formatted string. * * @param context The app context. * @param geofenceTransition The ID of the geofence transition. * @param triggeringGeofences The geofence(s) triggered. * @return The transition details formatted as String. */ 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; } /** * Maps geofence transition types to their human-readable equivalents. * * @param transitionType A transition type constant defined in Geofence * @return A String indicating the type of transition */ private String getTransitionString(int transitionType) { switch (transitionType) { case Geofence.GEOFENCE_TRANSITION_ENTER: return "Enter"; case Geofence.GEOFENCE_TRANSITION_EXIT: return "Exit"; default: return "Unknown"; } } }