Implementing Android’s new Activity Recognition Transition API

in #utopian-io6 years ago

Github  repo: https://github.com/Bethel-Eyo/Transitions

What you will learn: 

  In this tutorial, you will learn the following:  

  • Set up the Recognition Transition API Library 
  • Register for Updates
  • Process Events 
  • De-register Updates 
  • Review the code  

 Requirements  

  For this tutorial, you will need the following :

  • A PC (Personal Computer) with any operating such as Windows OS, Linux and MAC OSX 
  • An Integrated Development Environment such as Android Studio or any other IDE powered by IntelliJ for ease of coding 
  • Internet Access 
  • Basic Knowledge of Java, XML and how to navigate through the IDE of choice.  

  Difficulty  

  Intermediate 

 Overview 

 The provision for Mobile development on new Smart phones has helped to aid slick innovative technologies that has helped to better the lives of individuals. Our mobile devices have become indispensable in our daily activities, we basically take it everywhere we go; but until now it has been difficult for apps to adjust their experience to a user’s continually changing environment and activity. 

Activity transition API helps to solve this problem by providing a simple API that does all the processing for you and provides you with information that is specific to your needs. This tutorial shows you how to properly integrate this feature into your android app. 

 Step 1: Getting Started  

  To integrate the Transition API into your app, you are required to declare a dependency

  1. To make the Google Play services APIs available to your app. 

  • Open the build.gradle file inside your application module directory, 
  • Add a new build new rule under dependencies for the latest version of the play-services, as of this tutorial I used com.google.android.gms:play-services-location:15.0.1
  • Add this maven { url "https://maven.google.com" } reference to your top-level build.gradle file. 
  • Save the changes and sync the project with the gradle files. 

 2. Add a new permission to your AndroidManifest.xml file as shown below 

  To view the manifest file. Click here  

Step 2: Register for updates

 In order to get notified on activity transitions, you are required to implement an ActivityTransitionRequest (to specify the type of activity and transition) and a PendingIntent callback for receiving notifications. 

To create an ActivityTransitionRequest object, you have to create a list of ActivityTransition objects which will serve as a representation of the transition that you want to be notified about. 

Transition types include: 

  • ACTIVITY_TRANSITION_ENTER 
  • ACTIVITY_TRANSITION_EXIT   

 To know more about Activity Transitions, click here 

 Transition APIs support the following activities  

  • IN_VEHICLE 
  • ON_VEHICLE 
  • RUNNING 
  • STILL 
  • WORKING 

  To know more about these activities, click here 

 Here is a screenshot of how i created a list of ActivityTransitionObjects in my Transitions Android App  

 To view the activity from which I created the list of activity objects, click here.  

 Then you create can ActivityTransitionRequest object by passing in the list of transitions into the ActivityTransitionRequest class as shown below; 

  ActivityTransitionRequest request = new ActivityTransitionRequest(transitions);  

  You can also register for updates in activity transitions by passing in the instance of AcitvityTransitionrequest and your PendingIntent object to the requestActivityTransitionUpdates method which then returns a Task object which in turn helps you to check for for success or failure as shown in the code below; 

Task<Void> task =
           ActivityRecognition.getClient(this)
                   .requestActivityTransitionUpdates(request, mPendingIntent);
   task.addOnSuccessListener(
           new OnSuccessListener<Void>() {
               @Override
               public void onSuccess(Void result) {
                   Log.i(TAG, "Transitions Api was successfully registered.");
               }
           });

   task.addOnFailureListener(
           new OnFailureListener() {
               @Override
               public void onFailure(Exception e) {
                   Log.e(TAG, "Transitions Api could not be registered: " + e);
               }
           });
}

 After for activity transition updates successfully, the app begins to receive notifications in the registered PendingIntent.  

Step 3: Process Events

 On the occurrence of the activity transition, an intent callback is received. An ActivityTransitionResult object can be extracted from the intent and this will include a list of ActivityTransitionEvent objects. The events are ordered in a timely manner. For instance, if an app makes a request for the IN_VEHICLE activity type on the ACTIVITY_TRANSITION_ENTER and ACTIVITY_TRANSITION_EXIT transitions, then an ActivityTransitionEvent is received when the user starts driving, and another when the user transits to any other activity.  

 The callback can be implemented by creating a BroadcastReceiver and implementing the onReceive() method. as shown in the screenshot below

 Click here to view the file in which this was implemented

 Step 4: De-register Updates 

 After all the processes has been achieved and all activity transitions have been rightly implemented, you can de-register for activity transition updates by calling the removeActivityTransitionUpdates() method, of the ActivityRecognitionClient and passing in the PendingIntent object as the parameter as shown in the code below  

    

Task<Void> task =
 ActivityRecognition.getClient(context).removeActivityTransitionUpdates(myPendingIntent);

task.addOnSuccessListener(
 new OnSuccessListener<Void>() {
   @Override
   public void onSuccess(Void result) {
     myPendingIntent.cancel();
   }
 });

task.addOnFailureListener(
 new OnFailureListener() {
   @Override
   public void onFailure(Exception e) {
     Log.e("MYCOMPONENT", e.getMessage());
   }
 });

  Step 5: Review the code 

 A Basic Android app was implemented to better describe how the Activity Transition API can be implemented and how its features can be effectively utilized. To get a better understanding you can clone the repo with this link to your local machine see how it has been implemented and work through the code. 

 Proof of work done

   I hope Android developers find this tutorial helpful and get to work on implementing this new and amazing feature that makes activity transition detection smooth and easy. You can access the complete code for this tutorial on my github repo  

The apk of the app can also be downloaded here

Sort:  

Due to the above note by @portugalcoin, you have been banned temporarily from receiving Utopian reviews due to your plagiarism.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thank you for your contribution.
Your contribution will not be rewarded for the following reasons:

  • Submissions focused on the use of functions that are already well documented in the project documentation will be not be considered for potential reward.
  • I found a tutorial quite similar to yours here.

Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thanks @portugalcoin. The tutorial is almost like mine. But i rightly implemented it. In which they didn't. And i made a few tweaks to suit my implementation in the Transitions Android app. In which can help people understand better

Congratulations @betheleyo! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.16
TRX 0.17
JST 0.028
BTC 69086.63
ETH 2471.20
USDT 1.00
SBD 2.39