我一直在尝试将pendingIntent 作为任务的一部分传递给Activity Recognition Client,但是似乎没有调用pendingIntent。
我正在使用谷歌的活动识别客户端(https://developers.google.com/android/reference/com/google/android/gms/location/ActivityRecognitionClient)。我已经查看了示例代码(在这里找到:https ://github.com/googlesamples/android-play-location/blob/master/ActivityRecognition/app/src/main/java/com/google/android/gms/ location/sample/activityrecognition/MainActivity.java)但似乎看不到我的问题。
我的代码看起来像这样(我已经提取了部分类以使其更易于阅读):
public class SignIn extends AppCompatActivity {
private ActivityRecognitionClient mActivityRecognitionClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_in);
mActivityRecognitionClient = new ActivityRecognitionClient(this);
Task<Void> task = mActivityRecognitionClient.requestActivityUpdates(1000L, getActivityDetectionPendingIntent());
task.addOnSuccessListener(result -> Log.i("test", "test"));
task.addOnFailureListener(e -> Log.e("test", e.toString()));
}
private PendingIntent getActivityDetectionPendingIntent() {
Intent intent = new Intent(this, DetectedActivitiesIntentService.class);
return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
package com.example.kangaroute;
import android.app.IntentService;
import android.content.Intent;
import android.util.Log;
import com.google.android.gms.location.ActivityRecognitionResult;
import com.google.android.gms.location.DetectedActivity;
import java.util.ArrayList;
class DetectedActivitiesIntentService extends IntentService {
protected static final String TAG = "DetectedActivity";
public DetectedActivitiesIntentService(){
super(TAG);
}
@Override
public void onCreate(){
Log.i("test", "works");
super.onCreate();
}
@Override
protected void onHandleIntent(Intent intent) {
ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
// Get the list of the probable activities associated with the current state of the
// device. Each activity is associated with a confidence level, which is an int between
// 0 and 100.
ArrayList<DetectedActivity> detectedActivities = (ArrayList) result.getProbableActivities();
for (DetectedActivity activity : detectedActivities){
Log.i(TAG, activity.toString());
}
}
}
当我运行代码时,我希望看到“作品”被打印到 Logcat,因为我从类中的onCreate()
方法记录它DetectedActivitiesIntentService
。然而,我什么也没看到。
但是,“测试”是从与任务关联的 OnSuccessListener 记录的。当我记录结果时,什么都没有出现...