我使用的意識API,並創造了這樣的圍欄:刪除或註銷廣播接收機柵欄
AwarenessFence activityFence = DetectedActivityFence.during(DetectedActivityFence.STILL);
AwarenessFence headphoneFence = HeadphoneFence.during(HeadphoneState.PLUGGED_IN);
AwarenessFence stillWithHeadPhoneFence = AwarenessFence.and(activityFence, headphoneFence);
Intent intent = new Intent(Constants.ACTION_FENCE);
PendingIntent fencePendingIntent = PendingIntent.getBroadcast(((Activity) context), 0, intent, 0);
MyFenceReceiver mFenceBroadcastReceiver = new MyFenceReceiver();
((Activity) context).registerReceiver(mFenceBroadcastReceiver, new IntentFilter(Constants.ACTION_FENCE));
FenceUpdateRequest.Builder builder = new FenceUpdateRequest.Builder();
builder.addFence(Constants.IDLE_WITH_HEADPHONES_ON, stillWithHeadPhoneFence, fencePendingIntent);
Awareness.FenceApi.updateFences(googleApiClient, builder.build());
這是我的廣播接收器:
public void onReceive(Context context, Intent intent) {
if(TextUtils.equals(Constants.ACTION_FENCE, intent.getAction())) {
FenceState fenceState = FenceState.extract(intent);
if(TextUtils.equals(Constants.IDLE_WITH_HEADPHONES_ON, fenceState.getFenceKey())) {
if(fenceState.getCurrentState() == FenceState.TRUE) {
//
}
}
}
的問題是,一旦接收機第一次執行,我需要取消註冊或移除圍欄。我正在尋找刪除柵欄的方法,它需要一個GoogleApiClient實例。我怎樣才能在接收器中獲取該實例? P:我不能調用onStop cz中的remove,即使活動已經被銷燬,也可以觸發fence。
如果廣播接收器是本身未登記,則它won'tbe能聽其他圍欄。我只需要特定的圍欄就可以取消註冊。 –