Courtesy: GitHub

Recognizing the User's Current Activity

Demonstrates use of the ActivityRecognitionApi to recognize a user's current activity, such as walking, driving, or standing still.

Introduction

Demonstrates use of the ActivityRecognitionApi to recognize a user's current activity, such as walking, driving, or standing still.

Users can request activity updates by pressing the "Request Updates" button, and stop receiving updates using the "Remove Updates" button.

The sample uses an IntentService to process detected activity changes, which are sent using ActivityRecognitionResult objects. The IntentService gets a list of probable detected activities and broadcasts them through a BroadcastReceiver. See the DetectedActivity class for a list of DetectedActivity types. Each DetectedActivity is associdated with a confidence level, which is an int between 0 and 100.

To run this sample, location must be enabled.

Prerequisites

  • Android API Level >v9
  • Android Build Tools >v21
  • Google (Support) Repository

Getting Started

This sample uses the Gradle build system. To build this project, use the "gradlew build" command or use "Import Project" in Android Studio.

Support

If you've found an error in this sample, please file an issue: https://github.com/googlesamples/android-play-location/issues

Patches are encouraged, and may be submitted according to the instructions in CONTRIBUTING.md.

License

Copyright 2014 Google, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you 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.

compileSdkVersion 25

applicationId "com.google.android.gms.location.sample.activityrecognition"

minSdkVersion 16

targetSdkVersion 25

versionCode 1

versionName "1.0"

compile 'com.android.support:appcompat-v7:25.3.1'

testCompile 'junit:junit:4.12'

compile 'com.android.support:design:25.3.1'

compile 'com.google.android.gms:play-services-location:11.4.0'

compile 'com.google.code.gson:gson:2.7'

package com.google.android.gms.location.sample.activityrecognition

uses-permission

  • com.google.android.gms.permission.ACTIVITY_RECOGNITION

MainActivity

The entry point for interacting with activity recognition.

Adapter backed by a list of DetectedActivity objects.

Registers for activity recognition updates using {@link ActivityRecognitionClient#requestActivityUpdates(long, PendingIntent)}. Registers success and failure callbacks.

Removes activity recognition updates using {@link ActivityRecognitionClient#removeActivityUpdates(PendingIntent)}. Registers success and failure callbacks.

Gets a PendingIntent to be sent for each activity detection.

Ensures that only one button is enabled at any time. The Request Activity Updates button is enabled if the user hasn't yet requested activity updates. The Remove Activity Updates button is enabled if the user has requested activity updates.

Retrieves the boolean from SharedPreferences that tracks whether we are requesting activity updates.

Sets the boolean in SharedPreferences that tracks whether we are requesting activity updates.

Processes the list of freshly detected activities. Asks the adapter to update its list of DetectedActivities with new {@code DetectedActivity} objects reflecting the latest detected activities.

DetectedActivitiesIntentService

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.

IntentService for handling incoming intents that are generated as a result of requesting activity updates using {@link com.google.android.gms.location.ActivityRecognitionApi#requestActivityUpdates}.

This constructor is required, and calls the super IntentService(String) constructor with the name for a worker thread.

Handles incoming intents. @param intent The Intent is provided (inside a PendingIntent) when requestActivityUpdates() is called.