代码之家  ›  专栏  ›  技术社区  ›  Tiaan

无法调用OnHandleContent之外的方法或访问方法

  •  0
  • Tiaan  · 技术社区  · 6 年前

    这就是我被难倒的原因 我只是想在谷歌的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 我还有另一个可运行的循环读取此列表中最近的项。

    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 我觉得我快要疯了哈哈!

    0 回复  |  直到 6 年前