如何让Task调用pendingIntent

问题描述 投票:0回答:1

我一直在尝试将pendingIntent作为Task的一部分传递给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方法记录的。但是,我什么也看不见。

但是,从与Task关联的OnSuccessListener记录“test”。当我记录结果时,没有任何结果......

android android-intent android-pendingintent android-task
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.