2017-01-06 46 views
0

我已經創建了一個類和一個BroadcastReceiver,以便在行走或運行結束時從意識API獲取回調。 我沒有得到及時的回調,起初我以爲是因爲我註冊了一個'停止'回調,但是在將手機關機後,我確實收到了幾次回調!但這與我停止行走的時間相距甚遠。停車後至少5分鐘。 即使Google健身應用記錄活動,有時候我也不會收到回傳。Awareness API顯着延遲活動回調

由於我已經得到回調至少幾次,我知道註冊是好的。爲什麼這些電話會延遲並且有時會丟失?

對於背景參考,我將這些回調註冊在onStart主活動上,即啓動感知當時在活動的onstart內被調用。我從來沒有取消他們的註冊。 我不打算在生產中以這種方式使用它,它只是用於測試。再加上我的註冊應用程序上下文失敗的原因。

以下是我設置的Google客戶端和柵欄的註冊助手類。

public class AwarenessHelper { 


public static final String WALKING_ENDED_FENCE = "walkingEndedKey"; 
public static final String RUNNING_ENDED_FENCE = "runningEndedKey"; 
public static final String TYPE_2_WALKING = "duringWalkingKey"; 
public static final String TYPE_2_RUNNING = "duringRunningKey"; 

private String tag = AwarenessHelper.class.getSimpleName(); 

public void initiateAwareness(final Activity context) 
{ 
    final GoogleApiClient googleApiClient = buildClient(context); 
    Log.d(tag, "Initiating blocking connect"); 
    googleApiClient.registerConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { 
     @Override 
     public void onConnected(@Nullable Bundle bundle) { 
      if (googleApiClient.isConnected()) 
      { 
       Log.d(tag, "Client connected, initiating awareness fence registration"); 
       registerAwarenessFences(context, googleApiClient); 
      } 
      else 
      { 
       Log.d(tag, "Couldn't connect"); 
      } 

     } 

     @Override 
     public void onConnectionSuspended(int i) { 

     } 


    }); 

    googleApiClient.connect(); 
} 

private void registerAwarenessFences(Context context, GoogleApiClient mGoogleApiClient) { 
    Awareness.FenceApi.updateFences(
      mGoogleApiClient, 
      new FenceUpdateRequest.Builder() 
        .addFence(WALKING_ENDED_FENCE, DetectedActivityFence.stopping(DetectedActivityFence.WALKING), getBroadcastPendingIntent(context)) 
        .addFence(RUNNING_ENDED_FENCE, DetectedActivityFence.stopping(DetectedActivityFence.RUNNING), getBroadcastPendingIntent(context)) 
        .addFence(TYPE_2_WALKING, DetectedActivityFence.during(DetectedActivityFence.WALKING), getBroadcastPendingIntent(context)) 
        .addFence(TYPE_2_RUNNING, DetectedActivityFence.stopping(DetectedActivityFence.RUNNING), getBroadcastPendingIntent(context)) 
        .build()) 
      .setResultCallback(new ResultCallback<Status>() { 
       @Override 
       public void onResult(@NonNull Status status) { 
        if (status.isSuccess()) { 
         Log.i(tag, "Fence was successfully registered."); 
        } else { 
         Log.e(tag, "Fence could not be registered: " + status); 
        } 
       } 
      }); 
} 

private GoogleApiClient buildClient(final Activity activity) 
{ 
    GoogleApiClient client = new GoogleApiClient.Builder(activity) 
      .addApi(Awareness.API) 
      .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() { 
       @Override 
       public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
        if (connectionResult.hasResolution() && connectionResult.getErrorCode() == CommonStatusCodes.SIGN_IN_REQUIRED) 
        { 
         try { 
          connectionResult.startResolutionForResult(activity, GOOGLE_FIT_AUTHORIZATION_REQUEST_CODE); 
         } catch (IntentSender.SendIntentException e) { 
          e.printStackTrace(); 
         } 
        } 
       } 
      }) 
      .build(); 
    return client; 
} 

private PendingIntent getBroadcastPendingIntent(Context context) 
{ 
    Intent intent = new Intent(AWARENESS_BROADCAST_ACTION); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); 

    return pendingIntent; 
} 
} 

這裏的廣播接收器:

public class AwarenessHelper { 


    public static final String WALKING_ENDED_FENCE = "walkingEndedKey"; 
    public static final String RUNNING_ENDED_FENCE = "runningEndedKey"; 
    public static final String TYPE_2_WALKING = "duringWalkingKey"; 
    public static final String TYPE_2_RUNNING = "duringRunningKey"; 

    private String tag = AwarenessHelper.class.getSimpleName(); 

    public void initiateAwareness(final Activity context) 
    { 
     final GoogleApiClient googleApiClient = buildClient(context); 
     Log.d(tag, "Initiating blocking connect"); 
     googleApiClient.registerConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks() { 
      @Override 
      public void onConnected(@Nullable Bundle bundle) { 
       if (googleApiClient.isConnected()) 
       { 
        Log.d(tag, "Client connected, initiating awareness fence registration"); 
        registerAwarenessFences(context, googleApiClient); 
       } 
       else 
       { 
        Log.d(tag, "Couldn't connect"); 
       } 

      } 

      @Override 
      public void onConnectionSuspended(int i) { 

      } 


     }); 

     googleApiClient.connect(); 
    } 

    private void registerAwarenessFences(Context context, GoogleApiClient mGoogleApiClient) { 
     Awareness.FenceApi.updateFences(
       mGoogleApiClient, 
       new FenceUpdateRequest.Builder() 
         .addFence(WALKING_ENDED_FENCE, DetectedActivityFence.stopping(DetectedActivityFence.WALKING), getBroadcastPendingIntent(context)) 
         .addFence(RUNNING_ENDED_FENCE, DetectedActivityFence.stopping(DetectedActivityFence.RUNNING), getBroadcastPendingIntent(context)) 
         .addFence(TYPE_2_WALKING, DetectedActivityFence.during(DetectedActivityFence.WALKING), getBroadcastPendingIntent(context)) 
         .addFence(TYPE_2_RUNNING, DetectedActivityFence.stopping(DetectedActivityFence.RUNNING), getBroadcastPendingIntent(context)) 
         .build()) 
       .setResultCallback(new ResultCallback<Status>() { 
        @Override 
        public void onResult(@NonNull Status status) { 
         if (status.isSuccess()) { 
          Log.i(tag, "Fence was successfully registered."); 
         } else { 
          Log.e(tag, "Fence could not be registered: " + status); 
         } 
        } 
       }); 
    } 

    private GoogleApiClient buildClient(final Activity activity) 
    { 
     GoogleApiClient client = new GoogleApiClient.Builder(activity) 
       .addApi(Awareness.API) 
       .addOnConnectionFailedListener(new GoogleApiClient.OnConnectionFailedListener() { 
        @Override 
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 
         if (connectionResult.hasResolution() && connectionResult.getErrorCode() == CommonStatusCodes.SIGN_IN_REQUIRED) 
         { 
          try { 
           connectionResult.startResolutionForResult(activity, GOOGLE_FIT_AUTHORIZATION_REQUEST_CODE); 
          } catch (IntentSender.SendIntentException e) { 
           e.printStackTrace(); 
          } 
         } 
        } 
       }) 
       .build(); 
     return client; 
    } 

    private PendingIntent getBroadcastPendingIntent(Context context) 
    { 
     Intent intent = new Intent(AWARENESS_BROADCAST_ACTION); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); 

     return pendingIntent; 
    } 
} 

我得到的通知,但大規模的延遲後,有時根本沒有。 我開始多次活動,所以也許圍欄被反覆註冊?這是一個相關的事實嗎? 也是一個服務或廣播接收機上下文適合感知客戶端和圍牆的初始化?

+0

您是否找到了解決方案? – Dibzmania

回答

0

Awareness訂閱得到ActivityRecognition更新很少,所以在幾分鐘後得到響應並不是非常意外。

您還應該擔心在沒有註銷的情況下設置的防護層太多。

此外,沒有理由對每個圍欄分別使用pendingIntent;你可以有一個單一的pendingIntent並添加所有的防禦。使用圍欄關鍵字來區分每個圍欄的結果。再次,當它有意義時,請執行unregister。否則,即使您的應用程序消失,圍欄也可能會停留。