2017-04-07 46 views
2

我嘗試使用Google Awareness API實施活動柵欄。但用戶活動的變化未被檢測到。耳機圍欄按預期工作。活動柵欄在Awareness中不起作用API

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 is walking"); 
        break; 
       case FenceState.FALSE: 
        activityTextView.setText("User is 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 is running"); 
        break; 
       case FenceState.FALSE: 
        activityTextView.setText("User is 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> 

+0

您是否嘗試添加STILL Fence來檢查此柵欄是否被觸發? – MPhil

+0

我遇到了同樣的問題。我試圖對用戶活動的變化做出反應,但不知何故,它只會觸發一次,然後再次觸發。你有沒有設法得到任何見解? – saberrider

回答

0

如果耳機圍欄正在工作而且活動圍欄不在,也許您只是忘記在清單中添加權限?

<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"/> 
+0

我也包含了權限。出於某種原因,它沒有被顯示在問題中。我已經更新了它 –

0

我試圖用的,而不是地理界線意識API,我認爲這個問題是你如何測試你的代碼。 我很確定Awareness使用手機的傳感器,所以當我使用GPS模擬器來模擬散步時,它不會觸發任何事情,但是如果我使用我的真實手機並走路,BroadcastReceiver會被觸發。 但是,我不知道如何模擬仿真器上的「傳感器走動」!