Google ActivityRecognitionApi 未触发任何回调

Google ActivityRecognitionApi not firing any callbacks

GooglePlayServices 最近改变了您检测用户的方式 activity - 从使用 ActivityRecognitionClient 到 ActivityRecognitionApi。我遵循了我能找到的所有示例,但一定遗漏了一些东西。

在调用 GoogleApiClient.Builder(mContext).addApi(ActivityRecognition.API)...build() 之后,我预计会调用其中一种连接回调方法。我在所有回调方法中都有日志记录和断点,但是 none 曾经被调用过。

OnReceive() 和 CallClient() 按预期调用,清单文件包括:uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION"

我错过了什么?考虑到我到目前为止浪费的时间,我很想弄清楚这个问题。

(P.S。这是我自己的日志库 - 它不是问题的一部分)

public class BootAtStartupReceiver extends BroadcastReceiver implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener,  GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener{



private final String TAG = getClass().getName();
private PendingIntent mpIntent;
private static GoogleApiClient mGoogleApiClient;
private Context mContext;

@Override
public void onReceive(Context context, Intent intent) {

    Logging.log(Logging.enumLoggingLevel.d,TAG,"inside onReceive: " + intent.getAction());
    if ((intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) || (intent.getAction().equals(BroadcastIntentConstants.ACTIVITY_FIRST_TIME_CALL_SERVICE)))
    {
        mContext = context;
        callClient();
    }
}


private void callClient() {

    Logging.log(Logging.enumLoggingLevel.d,TAG,"inside callClient");

    int resp = GooglePlayServicesUtil.isGooglePlayServicesAvailable(mContext);
    if (resp == ConnectionResult.SUCCESS) {

        mGoogleApiClient = new GoogleApiClient.Builder(mContext)
                .addApi(ActivityRecognition.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

    } else {
        Logging.log(Logging.enumLoggingLevel.e, getClass().getName(), "Google play Services are not available.");
    }

}

@Override
public void onConnected(Bundle bundle) {
    Logging.log(Logging.enumLoggingLevel.d,TAG,"inside onConnected");

    Intent intent = new Intent(mContext, ActivityRecognitionIntentService.class);
    mpIntent = PendingIntent.getService(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mGoogleApiClient, 1000, mpIntent);
}

@Override
public void onDisconnected() {
    Logging.log(Logging.enumLoggingLevel.d,TAG,"inside onDisconnected");
}

@Override
public void onConnectionSuspended(int i) {
    Logging.log(Logging.enumLoggingLevel.d,TAG,"inside onConnectionSuspended");
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Logging.log(Logging.enumLoggingLevel.d,TAG,"inside onConnectionFailed");
}

}

构建后是否缺少对 connect() 的调用?除非您调用 connect,否则回调不会触发。

mGoogleApiClient.connect()