Java tutorial
/* * Copyright (C) 2013 The Android Open Source Project * * 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.nus.cs4222.isbtracker; import android.support.v4.content.LocalBroadcastManager; import com.google.android.gms.location.ActivityRecognitionResult; import com.google.android.gms.location.DetectedActivity; import android.app.IntentService; import android.content.Intent; import android.util.Log; /** * Service that receives ActivityRecognition updates. It receives updates * in the background, even if the main Activity is not visible. */ public class ActivityRecognitionIntentService extends IntentService { public ActivityRecognitionIntentService() { // Set the label for the service's background thread super("ActivityRecognitionIntentService"); } /** * Called when a new activity detection update is available. */ @Override protected void onHandleIntent(Intent intent) { // If the incoming intent contains an update if (ActivityRecognitionResult.hasResult(intent)) { // Get the update ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent); // Get the most probable activity DetectedActivity mostProbableActivity = result.getMostProbableActivity(); /* * Get the probability that this activity is the * the user's actual activity */ int confidence = mostProbableActivity.getConfidence(); /* * Get an integer describing the type of activity */ int activityType = mostProbableActivity.getType(); String activityName = getNameFromType(activityType); /* * At this point, you have retrieved all the information * for the current update. You can display this * information to the user in a notification, or * send it to an Activity or Service in a broadcast * Intent. */ Log.v("ActivityRecognitionResult", String.format("%s %d", activityName, confidence)); // Broadcast the result back to ScannerService // XXX: This class can probably be skipped entirely reportNewActivity(result); } else { /* * This implementation ignores intents that don't contain * an activity update. If you wish, you can report them as * errors. */ } } /** * Map detected activity types to strings * * @param activityType The detected activity type * @return A user-readable name for the type */ private String getNameFromType(int activityType) { switch (activityType) { case DetectedActivity.IN_VEHICLE: return "in_vehicle"; case DetectedActivity.ON_BICYCLE: return "on_bicycle"; case DetectedActivity.ON_FOOT: return "on_foot"; case DetectedActivity.STILL: return "still"; case DetectedActivity.UNKNOWN: return "unknown"; case DetectedActivity.TILTING: return "tilting"; } return "unknown"; } private void reportNewActivity(ActivityRecognitionResult result) { Intent intent = new Intent(ScannerService.UPDATE_TOPIC); intent.putExtra(Intent.EXTRA_SUBJECT, ActivityRecognitionHelper.ACTIVITY_RECOGNITION_EXTRA_SUBJECT); intent.putExtra(ActivityRecognitionHelper.ACTIVITY_RECOGNITION_RESULT, result); LocalBroadcastManager.getInstance(this).sendBroadcast(intent); } }