无法在onHandleIntent

时间:2018-07-27 08:31:29

标签: java android android-intent

所以这让我感到难过 我只是想在Google的API活动识别功能检测到某些参数时启动我的服务。 所以我扩展了IntentService并实现了GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener

Logcat确认服务工作正常(具有出乎意料的准确预测) 如果有人感兴趣,这就是我激活服务的方式:

    @Override
protected void onHandleIntent(@Nullable Intent intent) {

    if (ActivityRecognitionResult.hasResult(intent)){
        ActivityRecognitionResult recognitionResult = ActivityRecognitionResult.extractResult(intent);
        parseResults(recognitionResult.getProbableActivities());
        //My method to read the results from the list, logcat confirms it is working correctly

}

    @Override
public void onConnected(@Nullable Bundle bundle) {
    useActivityUpdates = true;
    Intent intent = new Intent(context, CA.class);
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(googleApiClient, 100, pendingIntent);
    Log.i("CA", "Connected");
}

因此,如您在onHandleIntent中所见,我正在调用parseResults函数。

    public long elapsedTimeGoogleAPI;
private void parseResults( List<DetectedActivity> detectedActivities){
    PredictedActivity newActivity = new PredictedActivity();
    newActivity.bicycle = 0;
    newActivity.onFoot = 0;
    newActivity.unknown = 0;
    newActivity.vehicle = 0;
    newActivity.walking = 0;
    newActivity.running = 0;
    newActivity.still = 0;
    Log.i("CAActivityDetect", "NewActivity");

    newActivity.elapsedTime = System.currentTimeMillis() - elapsedTimeGoogleAPI;
    for (DetectedActivity dt : detectedActivities){
        switch (dt.getType()){
            case DetectedActivity.IN_VEHICLE:
                newActivity.vehicle = dt.getConfidence();
                Log.i("CAActivityDetect", "v: " + dt.getConfidence());

                break;
            case DetectedActivity.ON_BICYCLE:
                newActivity.bicycle = dt.getConfidence();
                Log.i("CAActivityDetect", "b: " + dt.getConfidence());

                break;
            case DetectedActivity.WALKING:
                newActivity.walking = dt.getConfidence();
                Log.i("CAActivityDetect", "w: " + dt.getConfidence());

                break;
            case DetectedActivity.ON_FOOT:
                newActivity.onFoot = dt.getConfidence();
                Log.i("CAActivityDetect", "of: " + dt.getConfidence());

                break;
            case DetectedActivity.RUNNING:
                newActivity.running = dt.getConfidence();
                Log.i("CAActivityDetect", "r: " + dt.getConfidence());

                break;
            case DetectedActivity.UNKNOWN:
                newActivity.unknown = dt.getConfidence();
                Log.i("CAActivityDetect", "u: " + dt.getConfidence());

                break;
            case DetectedActivity.STILL:
                newActivity.still = dt.getConfidence();
                Log.i("CAActivityDetect", "s: " + dt.getConfidence());

                break;
        }
    }
    if (predictedActivities == null){
        predictedActivities = new ArrayList<>();
        Log.i("CAActivityDetect", "predictedActivities == null New ArrayList");

    }
    predictedActivities.add(newActivity);
}

因此,在构造和激活API的类中的另一种方法中,我初始化了PredictedActivity列表,并在其中放置了一个日志以供参考... 我还有另一个Runnable循环,从该列表中读取最新的项目。

这些是logcat结果:

130|shell@tilapia:/ $ logcat | grep "CAActivityDetect"
I/CAActivityDetect(19902): New ArrayList
I/CAActivityDetect(19902): Attempt connect
I/CAActivityDetect(19902): Connected
I/CAActivityDetect(19902): NewActivity
I/CAActivityDetect(19902): s: 100
I/CAActivityDetect(19902): predictedActivities == null New ArrayList
I/CAActivityDetect(19902): NewActivity
I/CAActivityDetect(19902): s: 100
I/CAActivityDetect(19902): predictedActivities == null New ArrayList
I/CAActivityDetect(19902): NewActivity
I/CAActivityDetect(19902): s: 100
I/CAActivityDetect(19902): predictedActivities == null New ArrayList
I/CAActivityDetect(19902): NewActivity
I/CAActivityDetect(19902): s: 100
I/CAActivityDetect(19902): predictedActivities == null New ArrayList

因此,这使我相信,尽管所有这些代码都位于类的同一实例中,但是某种程度上,IntentService无法以某种方式编辑该类使用的信息... parseResults被使用专门由IntentService方法提供并且可以正常工作,但是当我每次尝试添加到主程序使用的列表时,每次它创建一个新的ArrayList时,就像它无法访问上次使用的相同变量一样,或者该类使用。 我觉得我要疯了哈哈!

0 个答案:

没有答案