android在后台检测ActivityRecognition

时间:2015-12-09 14:13:07

标签: android service

我想在应用关闭时检测用户用户在后台的活动。有一个使用Google API for Android的ActivityRecognition的示例。 在示例中,识别由Activity启动,我实现了服务。 它适用于大多数设备,但不适用于具有api 4.2.x的设备。

public class RecognitionService extends Service implements GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    ResultCallback<Status> {
private static final String TAG = RecognitionService.class.getSimpleName();
public final static int DETECTION_INTERVAL_IN_MILLISECONDS = 60_000;
private GoogleApiClient googleApiClient;

public RecognitionService() {
    super();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    initLocationClient();
    return START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

private void initLocationClient() {
    if (googleApiClient == null) {
        googleApiClient = new GoogleApiClient
                .Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(ActivityRecognition.API)
                .build();
    }

    if (!googleApiClient.isConnected() || !googleApiClient.isConnecting()) {
        googleApiClient.connect();
    }
}

@Override
public void onConnected(Bundle bundle) {
    Log.i(TAG, "Connected to GoogleApiClient");
    if (googleApiClient != null && googleApiClient.isConnected()) {
        ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
                googleApiClient,
                DETECTION_INTERVAL_IN_MILLISECONDS,
                getActivityDetectionPendingIntent()
        ).setResultCallback(this);
    }
}

@Override
public void onConnectionSuspended(int i) {
    Log.i(TAG, "Connection suspended");
    googleApiClient.connect();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + connectionResult.getErrorCode());
}

private PendingIntent getActivityDetectionPendingIntent() {
    Intent intent = new Intent(this, DetectedActivitiesIntentService.class);

    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
    // requestActivityUpdates() and removeActivityUpdates().
    return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

@Override
public void onResult(Status status) {
    Log.i(TAG, "onResult = " + status.getStatusMessage());
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.i(TAG, "onDestroy");
    ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(
            googleApiClient,
            getActivityDetectionPendingIntent()
    ).setResultCallback(this);
}
}

不会调用onConnected,onConnectionSuspended和onConnectionFailed方法。

你对这个案子有什么看法吗?

1 个答案:

答案 0 :(得分:0)

使用isGooglePlayServicesAvailable()确认设备安装了正确版本的GooglePlayServices:

    GoogleApiAvailability apiAvail = GoogleApiAvailability.getInstance();
    int errorCode = apiAvail.isGooglePlayServicesAvailable(this);

    if (errorCode == ConnectionResult.SUCCESS) {
        // okay
    } else {
        // not available; start processing to resolve
    }

虽然在向onConnectionFailed()发出连接请求后,应报告播放服务不可用,但使用isGooglePlayServicesAvailable()提供了另一种检查可用性的方法。

SO question解决了对ActivityRecognition的支持。