如何让Task呼叫未决的Intent

时间:2019-04-20 01:56:04

标签: android android-intent android-pendingintent android-task

我一直在尝试将任务的一部分的未完成的意图传递给活动识别客户端,但是似乎未调用未完成的意图。

我正在使用Google的活动识别客户端(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());
        }

    }
}

当我运行代码时,我希望看到从onCreate()类的DetectedActivitiesIntentService方法记录“作品”时将其记录到Logcat。但是,我什么也没看到。

但是,“ test”是从与任务相关联的OnSuccessListener记录的。当我记录结果时,什么都没有出现...

1 个答案:

答案 0 :(得分:0)

大声猜猜谁忘了放“公共课”,而只是放“课”。解决了!