android - Activity Fence not working in Awareness API -


i tried implementing activity fence using google awareness api. changes in user's activity not getting detected. headphone fence works expected though.

activityfenceactivity

public class activityfenceactivity extends appcompatactivity implements googleapiclient.connectioncallbacks {  private static final string fence_receiver_action = "fence_receive"; private static final string fence_walking_key = "walkingkey"; private static final string fence_running_key = "runningkey"; private static final string tag = activityfenceactivity.class.getsimplename();  private googleapiclient googleapiclient; private textview activitytextview; private broadcastreceiver activityfencereceiver = new broadcastreceiver() {     @override     public void onreceive(final context context, final intent intent) {         toast.maketext(context, "recieved", toast.length_short).show();         fencestate fencestate = fencestate.extract(intent);          if (textutils.equals(fencestate.getfencekey(), fence_walking_key)) {             switch (fencestate.getcurrentstate()) {                 case fencestate.true:                     activitytextview.settext("user walking");                     break;                 case fencestate.false:                     activitytextview.settext("user not walking");                     break;                 case fencestate.unknown:                     activitytextview.settext("activity state unknown");                     break;             }         } else if (textutils.equals(fencestate.getfencekey(), fence_running_key)) {             switch (fencestate.getcurrentstate()) {                 case fencestate.true:                     activitytextview.settext("user running");                     break;                 case fencestate.false:                     activitytextview.settext("user not running");                     break;                 case fencestate.unknown:                     activitytextview.settext("activity state unknown");                     break;             }         }     } };  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_activity_fence);      activitytextview = (textview) findviewbyid(r.id.activitytextview);      googleapiclient = new googleapiclient.builder(activityfenceactivity.this)             .addapi(awareness.api)             .addconnectioncallbacks(this)             .build();     googleapiclient.connect();      findviewbyid(r.id.register_fence).setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             registeractivityfence();         }     });      findviewbyid(r.id.unregister_fence).setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             unregisteractivityfence();         }     });   }  @override public void onconnected(@nullable final bundle bundle) {     log.d(tag, "google api connected");  }  @override public void onconnectionsuspended(final int i) {     log.d(tag, "google api connection suspended");  }  @override protected void onstart() {     super.onstart();     registerreceiver(activityfencereceiver, new intentfilter(fence_receiver_action)); }  @override protected void onstop() {     super.onstop();      unregisterreceiver(activityfencereceiver);     unregisteractivityfence(); }  private void registeractivityfence() {     awarenessfence walkingfence = detectedactivityfence.during(detectedactivityfence.walking);     awarenessfence runningfence = detectedactivityfence.during(detectedactivityfence.running);      pendingintent fencependingintent = pendingintent.getbroadcast(this,             0,             new intent(fence_receiver_action),             0);      awareness.fenceapi.updatefences(googleapiclient, new fenceupdaterequest.builder()             .addfence(fence_walking_key, walkingfence, fencependingintent).build())             .setresultcallback(new resultcallbacks<status>() {                 @override                 public void onsuccess(@nonnull final status status) {                     toast.maketext(activityfenceactivity.this,                             "fence registered successfully",                             toast.length_short).show();                 }                  @override                 public void onfailure(@nonnull final status status) {                     toast.maketext(activityfenceactivity.this,                             "cannot register activity fence.",                             toast.length_short).show();                 }             });      awareness.fenceapi.updatefences(googleapiclient, new fenceupdaterequest.builder()             .addfence(fence_running_key, runningfence, fencependingintent).build());  }  private void unregisteractivityfence() {     awareness.fenceapi.updatefences(             googleapiclient,             new fenceupdaterequest.builder()                     .removefence(fence_walking_key)                     .removefence(fence_running_key)                     .build()).setresultcallback(new resultcallbacks<status>() {         @override         public void onsuccess(@nonnull status status) {             toast.maketext(activityfenceactivity.this,                     "fence unregistered successfully.",                     toast.length_short).show();         }          @override         public void onfailure(@nonnull status status) {             toast.maketext(activityfenceactivity.this,                     "cannot unregister headphone fence.",                     toast.length_short).show();         }     }); } 

}

androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?> 

<uses-permission android:name="com.google.android.gms.permission.activity_recognition" />  <application     android:allowbackup="true"     android:icon="@mipmap/ic_launcher"     android:label="@string/app_name"     android:roundicon="@mipmap/ic_launcher_round"     android:supportsrtl="true"     android:theme="@style/apptheme">     <activity android:name=".mainactivity">         <intent-filter>             <action android:name="android.intent.action.main" />              <category android:name="android.intent.category.launcher" />         </intent-filter>     </activity>      <meta-data         android:name="com.google.android.awareness.api_key"         android:value="aizasybemjlfc87xrup2fnfynsddy3qrui1hihs" />  </application> 

if headphone fence working , activity fence not, maybe forgot add permission in manifest?

<uses-permission android:name="com.google.android.gms.permission.activity_recognition"/> 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -