Java tutorial
/** * Copyright 2017 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.pingstersapp.LocationService; 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.location.Location; import android.preference.PreferenceManager; import android.support.v4.app.NotificationCompat; import android.support.v4.app.TaskStackBuilder; import com.pingstersapp.MainActivity; import com.pingstersapp.R; import java.text.DateFormat; import java.util.Date; import java.util.List; /** * Utility methods used in this sample. */ public class Utils { public final static String KEY_LOCATION_UPDATES_REQUESTED = "location-updates-requested"; public final static String KEY_LOCATION_UPDATES_RESULT = "location-update-result"; public static void setRequestingLocationUpdates(Context context, boolean value) { PreferenceManager.getDefaultSharedPreferences(context).edit() .putBoolean(KEY_LOCATION_UPDATES_REQUESTED, value).apply(); } public static boolean getRequestingLocationUpdates(Context context) { return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(KEY_LOCATION_UPDATES_REQUESTED, false); } /** * Posts a notification in the notification bar when a transition is detected. * If the user clicks the notification, control goes to the MainActivity. */ static void sendNotification(Context context, String notificationDetails) { // Create an explicit content Intent that starts the main Activity. Intent notificationIntent = new Intent(context, MainActivity.class); notificationIntent.putExtra("from_notification", true); // 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.mipmap.ic_launcher) // In a real app, you may want to use a library like Volley // to decode the Bitmap. .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher)) .setColor(Color.RED).setContentTitle("Location update").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()); } /** * Returns the title for reporting about a list of {@link Location} objects. * * @param context The {@link Context}. */ static String getLocationResultTitle(Context context, List<Location> locations) { String numLocationsReported = java.lang.Integer.toString(locations.size()); return numLocationsReported + ": " + DateFormat.getDateTimeInstance().format(new Date()); } /** * Returns te text for reporting about a list of {@link Location} objects. * * @param locations List of {@link Location}s. */ private static String getLocationResultText(Context context, List<Location> locations) { if (locations.isEmpty()) { return "unknown location"; } StringBuilder sb = new StringBuilder(); for (Location location : locations) { sb.append("("); sb.append(location.getLatitude()); sb.append(", "); sb.append(location.getLongitude()); sb.append(")"); sb.append("\n"); } return sb.toString(); } static void setLocationUpdatesResult(Context context, List<Location> locations) { PreferenceManager.getDefaultSharedPreferences(context).edit().putString(KEY_LOCATION_UPDATES_RESULT, getLocationResultTitle(context, locations) + "\n" + getLocationResultText(context, locations)) .apply(); } public static String getLocationUpdatesResult(Context context) { return PreferenceManager.getDefaultSharedPreferences(context).getString(KEY_LOCATION_UPDATES_RESULT, ""); } }