Google ActivityRecognitionApi不会触发任何回调

时间:2015-01-18 16:11:31

标签: android google-play-services activity-recognition

GooglePlayServices最近改变了您检测用户活动的方式 - 从使用ActivityRecognitionClient到ActivityRecognitionApi。我已经按照我能找到的所有例子,但肯定错过了一些东西。

在调用GoogleApiClient.Builder(mContext).addApi(ActivityRecognition.API)... build()后,我期待调用其中一个连接回调方法。我在所有回调方法中都有记录和断点,但它们都没有被调用过。

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

我错过了什么?鉴于到目前为止浪费的时间,我希望能够深究这一点。

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

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");
}

}

1 个答案:

答案 0 :(得分:0)

你是否在构建后错过了对connect()的调用?除非你打电话给连接,否则回调不会激活。

mGoogleApiClient.connect()