ActivityRecognitionClient的活动识别无法解析,android中的错误

时间:2015-01-10 06:02:30

标签: android performance android-intent android-activity geolocation

我正在尝试在android中使用ActivityRecognition来检测用户是否正在开车。

http://www.kpbird.com/2013/07/android-activityrecognition-example.html:这是我一直在尝试的示例代码......

但是当我写ActivityRecognitionClient client;时,我的IDE(Android Studio)说它无法解析它,并将其标记为红色,但ActivityRecognition已解决。

所以我手动导入com.google.android.gms.location.ActivityRecognitionClient;,但它也被标记为红色,我已经安装了所有谷歌Api和播放服务,

请帮我搞定! :)

这是我的Gradle。

apply plugin: 'com.android.application'

android {
compileSdkVersion "Google Inc.:Google APIs:21"
buildToolsVersion "21.1.1"

defaultConfig {
    applicationId "interrupt.smart.com.smartinterrupt"
    minSdkVersion 16
    targetSdkVersion 21
    versionCode 1
    versionName "1.0"
}
 buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])


compile 'com.android.support:cardview-v7:21.0.+'
compile 'com.android.support:recyclerview-v7:21.0.+'
compile 'com.android.support:appcompat-v7:21.0.+'
compile 'com.google.android.gms:play-services:6.5.87'

compile 'com.google.android.gms:play-services-location:6.5.87'
}

1 个答案:

答案 0 :(得分:5)

根据Google Play Services 6.5 highlights

  

不推荐使用的客户端 - 不推荐使用ActivityRecognitionClientLocationClientPlusClient类。如果您在应用中使用这些API并想要调用Google Play服务6.5或更高版本的API,则必须切换到使用GoogleApiClient的新编程模型。有关使用GoogleApiClient的详细信息,请参阅Accessing Google APIs

     

使用这些API而不是弃用的API:

     
      
  • 如果您之前使用的是ActivityRecognitionClient,请改为呼叫ActivityRecognition
  •   

假设您已连接GoogleApiClient

PendingResult<Status> result = ActivityRecognition.ActivityRecognitionApi
    .requestActivityUpdates(
        googleApiClient,         // your connected GoogleApiClient
        detectionIntervalMillis, // how often you want callbacks
        callbackIntent);         // the PendingIntent which will 
                                 //   receive updated activities

// Callback is asynchronous. Use await() on a background thread or listen for
// the ResultCallback
result.setResultCallback(new ResultCallback<Status>() {
    void onResult(Status status) {
        if (status.isSuccess()) {
            // Successfully registered
        } else if (status.hasResolution()) {
            // Google provides a way to fix the issue
            status.startResolutionForResult(
                activity,     // your current activity used to receive the result
                RESULT_CODE); // the result code you'll look for in your
                              // onActivityResult method to retry registering
        } else {
            // No recovery. Weep softly or inform the user.
            Log.e(TAG, "Registering failed: " + status.getStatusMessage());
        }
   }
});