{"id":18633,"date":"2015-03-29T12:10:54","date_gmt":"2015-03-29T06:40:54","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=18633"},"modified":"2015-03-29T12:10:54","modified_gmt":"2015-03-29T06:40:54","slug":"activity-recognition-using-new-googleapiclient-activityrecognitionclient-is-deprecated-in-december-2014","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/activity-recognition-using-new-googleapiclient-activityrecognitionclient-is-deprecated-in-december-2014\/","title":{"rendered":"Activity Recognition using new GoogleApiClient [ActivityRecognitionClient is deprecated in december 2014]"},"content":{"rendered":"<p style=\"text-align: justify\"><a href=\"https:\/\/developer.android.com\/google\/play-services\/index.html\" target=\"_blank\">As per Google play services 6.5 Highlights :<\/a> The\u00a0<del>ActivityRecognitionClient<\/del>,\u00a0<del>LocationClient<\/del>, and\u00a0<del>PlusClientclasses<\/del> are deprecated. If you used those APIs in your app and want to call Google Play services 6.5 or higher APIs, you must switch to the new programming model that utilizes <strong>GoogleApiClient<\/strong>.<\/p>\n<p lang=\"zxx\" style=\"text-align: justify\" align=\"left\">As per docs, GoogleApiClient is able to\u00a0add the multiple APIs \u00a0into the single client.<br \/>\ni.e.\u00a0\u00a0&#8211; In one GoogleApiClient, You can add both \u00a0LocationServices.API and ActivityRecognition.API, or different API client.<\/p>\n<h6><span class=\"highlight\">What is mean by Google Activity Recognition?<\/span><\/h6>\n<p>For the Android world, It comes with a lot of new ideas for the application. Activity Recognition API recognize various activities like :<\/p>\n<ul>\n<li style=\"text-align: justify\">The device is in a vehicle, such as a car.<\/li>\n<li style=\"text-align: justify\">The device is on a bicycle.<\/li>\n<li style=\"text-align: justify\">The device is on a user who is walking or running.<\/li>\n<li style=\"text-align: justify\">The device is still (not moving).<\/li>\n<li style=\"text-align: justify\">The device angle relative to gravity changed significantly and so on.<\/li>\n<\/ul>\n<h6><span class=\"highlight\"><br \/>\nHow well does it work?<\/span><\/h6>\n<p>In my personal testing, I get following thing. When i am walking from step-down to vehicle, there is some delay (approximate 2-4 second).<\/p>\n<h6><span class=\"highlight\">Useful Methods<\/span><\/h6>\n<ol>\n<li>ActivityRecognitionClient\n<ul>\n<li>connect()<\/li>\n<li>disconnect()<\/li>\n<li>requestActivityUpdates()<\/li>\n<li>removeActivityUpdates()<\/li>\n<\/ul>\n<\/li>\n<li>ActivityRecognitionResult\n<ul>\n<li>hasResult()<\/li>\n<li>extractResult()<\/li>\n<li>getMostProbableActivity().getType()<\/li>\n<li>getMostProbableActivity().getConfidence()<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h6><span class=\"highlight\"><br \/>Step to Implement<\/span><\/h6>\n<p><strong>Step 1:<\/strong> Add Google Play Service(6.5 or higher) library \u00a0in your project.<\/p>\n<p><strong>Step 2:<\/strong>\u00a0Add required permission in the Manifest.<\/p>\n<p>[xml]<br \/>\n&lt;uses-permission android:name=&quot;com.google.android.gms.permission.ACTIVITY_RECOGNITION&quot; \/&gt;<br \/>\n[\/xml]<\/p>\n<p><strong>Step 3:<\/strong> Implement <em>GoogleApiClient.ConnectionCallbacks<\/em> and <em>GoogleApiClient.OnConnectionFailedListener<\/em> interface.<\/p>\n<p>[java]<br \/>\nGoogleApiClient.ConnectionCallbacks connectionCallbacks = new GoogleApiClient.ConnectionCallbacks() {<br \/>\n            @Override<br \/>\n            public void onConnected(Bundle bundle) {<br \/>\n            }<\/p>\n<p>            @Override<br \/>\n            public void onConnectionSuspended(int i) {<br \/>\n            }<br \/>\n        };<br \/>\n[\/java]<\/p>\n<p>[java]<br \/>\nGoogleApiClient.OnConnectionFailedListener onConnectionFailedListener = new GoogleApiClient.OnConnectionFailedListener() {<br \/>\n            @Override<br \/>\n            public void onConnectionFailed(ConnectionResult connectionResult) {<\/p>\n<p>            }<br \/>\n        };<br \/>\n[\/java]<\/p>\n<p><strong>Step 4:<\/strong> Connect the GoogleApiClient with ActivityRecognition.API.<\/p>\n<p>[java]<br \/>\nGoogleApiClient googleApiClient = new GoogleApiClient.Builder(this)<br \/>\n                .addApi(ActivityRecognition.API)<br \/>\n                .addConnectionCallbacks(this) \/\/this is refer to connectionCallbacks interface implementation.<br \/>\n                .addOnConnectionFailedListener(this) \/\/this is refer to onConnectionFailedListener interface implementation.<br \/>\n                .build();<\/p>\n<p>googleApiClient.connect();<\/p>\n<p>[\/java]<\/p>\n<p><strong>Step 5:<\/strong> Create IntentService and override onHandleIntent(Intent intent) method. Don&#8217;t forget to add this to your manifest file.<\/p>\n<p>[java]<br \/>\npublic class ActivityRecognizationIntentService extends IntentService {<\/p>\n<p>    public ActivityRecognizationIntentService() {<br \/>\n        super(&quot;ActivityRecognizationIntentService&quot;);<br \/>\n    }<\/p>\n<p>    @Override<br \/>\n    protected void onHandleIntent(Intent intent) {<br \/>\n        if(ActivityRecognitionResult.hasResult(intent)){<br \/>\n            ActivityRecognitionResult activityRecognitionResult = ActivityRecognitionResult.extractResult(intent);<br \/>\n            DetectedActivity detectedActivity = activityRecognitionResult.getMostProbableActivity();<\/p>\n<p>            int confidence = detectedActivity.getConfidence();<br \/>\n            String recognizeActivity = getActivityName(detectedActivity.getType());<\/p>\n<p>            Log.d(&quot;Himanshu&quot;,&quot;Confidence : &quot; + confidence);<br \/>\n            Log.d(&quot;Himanshu&quot;,&quot;RecognizeActivity : &quot; + recognizeActivity);<\/p>\n<p>        }<br \/>\n    }<\/p>\n<p>    private String getActivityName(int activityType){<br \/>\n        switch (activityType){<br \/>\n            case DetectedActivity.IN_VEHICLE:<br \/>\n                return &quot;IN_VEHICLE&quot;;<br \/>\n            case DetectedActivity.ON_BICYCLE:<br \/>\n                return &quot;ON_BICYCLE&quot;;<br \/>\n            case DetectedActivity.STILL:<br \/>\n                return &quot;STILL&quot;;<br \/>\n            case DetectedActivity.TILTING:<br \/>\n                return &quot;TILTING&quot;;<br \/>\n            case DetectedActivity.RUNNING:<br \/>\n                return &quot;RUNNING&quot;;<br \/>\n            case DetectedActivity.ON_FOOT:<br \/>\n                return &quot;ON_FOOT&quot;;<br \/>\n            case DetectedActivity.WALKING:<br \/>\n                return &quot;WALKING&quot;;<br \/>\n            case DetectedActivity.UNKNOWN:<br \/>\n                return &quot;UNKNOWN&quot;;<br \/>\n        }<br \/>\n        return &quot;&quot;;<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p><strong>Step 6:<\/strong> Create a pending intent and pass it in ActivityRecognitionApi.requestActivityUpdates() as a arguments.<\/p>\n<p>[java]<br \/>\n \/\/Before request API request, GoogleApiClient must be in connected mode.<br \/>\nIntent intent = new Intent(this,ActivityRecognizationIntentService.class);<br \/>\nPendingIntent pendingIntent = PendingIntent.getService(this,1,intent,PendingIntent.FLAG_UPDATE_CURRENT);<\/p>\n<p>ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(googleApiClient,1,pendingIntent);<br \/>\n[\/java]<\/p>\n<p lang=\"zxx\" style=\"text-align: justify\" align=\"left\">Now, I feel really excited when I think about use-case of GoogleApiClient APIs in Android world. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>As per Google play services 6.5 Highlights : The\u00a0ActivityRecognitionClient,\u00a0LocationClient, and\u00a0PlusClientclasses are deprecated. If you used those APIs in your app and want to call Google Play services 6.5 or higher APIs, you must switch to the new programming model that utilizes GoogleApiClient. As per docs, GoogleApiClient is able to\u00a0add the multiple APIs \u00a0into the single [&hellip;]<\/p>\n","protected":false},"author":126,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":8},"categories":[518],"tags":[1726,1728,1727],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18633"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/126"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=18633"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/18633\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=18633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=18633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=18633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}