0

I'm using Google's activity recognition API, and I've created an IntentService to handle the activity updates. I'm supposed to receive updates every 5 seconds, and so I was when I first installed and booted the app on my phone. Then I made some changes and now I'm only receiving an update ONCE. I'm not sure why this is happening as it was working fine just a moment ago. Here's my code:

Connecting to the API:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    activityNumber = (TextView) findViewById(R.id.activityNumber);
    File root = Environment.getExternalStorageDirectory();
    csvFile = new File(root,"ActivityRecognition.csv");
    activityList = new ArrayList<>();

    mApiClient = new GoogleApiClient.Builder(this)
            .addApi(ActivityRecognition.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mApiClient.connect();

    activityReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            activityList.add(new RecognizedActivity(intent.getLongExtra(EXTRA_TIMESTAMP, 0), intent.getStringExtra(EXTRA_ACTIVITY), intent.getIntExtra(EXTRA_CONFIDENCE, 0)));
            activityNumber.setText(String.valueOf(activityList.size()));
        }
    };

    IntentFilter filter = new IntentFilter();
    filter.addAction( ACTION_ACTIVITY_RECOGNIZED );

    registerReceiver(activityReceiver, filter);


}

 @Override
public void onConnected(@Nullable Bundle bundle) {
    Log.i("Connected", "NOW");
    Intent i = new Intent(this,ActivityRecognitionService.class);
    PendingIntent pi = PendingIntent.getService(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
    ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates( mApiClient, 5000, pi);
}

My IntentService:

   public ActivityRecognitionService() {
    super("ActivityRecognitionService");
}

@Override
protected void onHandleIntent(Intent intent) {
    Log.i("HandleIntent", "NOW");
    if (intent != null) {
        if(ActivityRecognitionResult.hasResult(intent)) {
            ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
            handleDetectedActivities(result.getProbableActivities());
        }
    }
}

private void handleDetectedActivities(List<DetectedActivity> probableActivities) {
    Log.i("HandleActivity", "NOW");
    calendar = Calendar.getInstance();
    activityIntent = new Intent();
    activityIntent.setAction(MainActivity.ACTION_ACTIVITY_RECOGNIZED);
    for( DetectedActivity activity : probableActivities ) {
        switch( activity.getType() ) {
            case DetectedActivity.IN_VEHICLE: {
                activityIntent.putExtra(MainActivity.EXTRA_TIMESTAMP, calendar.getTime().getTime());
                activityIntent.putExtra(MainActivity.EXTRA_ACTIVITY, "In Vehicle");
                activityIntent.putExtra(MainActivity.EXTRA_CONFIDENCE, activity.getConfidence());
                break;
            }
            case DetectedActivity.ON_BICYCLE: {
                activityIntent.putExtra(MainActivity.EXTRA_TIMESTAMP, calendar.getTime().getTime());
                activityIntent.putExtra(MainActivity.EXTRA_ACTIVITY, "On Bicycle");
                activityIntent.putExtra(MainActivity.EXTRA_CONFIDENCE, activity.getConfidence());
                break;
            }
            case DetectedActivity.ON_FOOT: {
                activityIntent.putExtra(MainActivity.EXTRA_TIMESTAMP, calendar.getTime().getTime());
                activityIntent.putExtra(MainActivity.EXTRA_ACTIVITY, "On Foot");
                activityIntent.putExtra(MainActivity.EXTRA_CONFIDENCE, activity.getConfidence());
                break;
            }
            case DetectedActivity.RUNNING: {
                activityIntent.putExtra(MainActivity.EXTRA_TIMESTAMP, calendar.getTime().getTime());
                activityIntent.putExtra(MainActivity.EXTRA_ACTIVITY, "Running");
                activityIntent.putExtra(MainActivity.EXTRA_CONFIDENCE, activity.getConfidence());
                break;
            }
            case DetectedActivity.STILL: {
                activityIntent.putExtra(MainActivity.EXTRA_TIMESTAMP, calendar.getTime().getTime());
                activityIntent.putExtra(MainActivity.EXTRA_ACTIVITY, "Still");
                activityIntent.putExtra(MainActivity.EXTRA_CONFIDENCE, activity.getConfidence());
                break;
            }
            case DetectedActivity.TILTING: {
                activityIntent.putExtra(MainActivity.EXTRA_TIMESTAMP, calendar.getTime().getTime());
                activityIntent.putExtra(MainActivity.EXTRA_ACTIVITY, "Tilting");
                activityIntent.putExtra(MainActivity.EXTRA_CONFIDENCE, activity.getConfidence());
                break;
            }
            case DetectedActivity.WALKING: {
                activityIntent.putExtra(MainActivity.EXTRA_TIMESTAMP, calendar.getTime().getTime());
                activityIntent.putExtra(MainActivity.EXTRA_ACTIVITY, "Walking");
                activityIntent.putExtra(MainActivity.EXTRA_CONFIDENCE, activity.getConfidence());
                break;
            }
            case DetectedActivity.UNKNOWN: {
                activityIntent.putExtra(MainActivity.EXTRA_TIMESTAMP, calendar.getTime().getTime());
                activityIntent.putExtra(MainActivity.EXTRA_ACTIVITY, "Unknown");
                activityIntent.putExtra(MainActivity.EXTRA_CONFIDENCE, activity.getConfidence());
                break;
            }
        }
        Log.i("BROADCAST SENT", "NOW");
        sendBroadcast(activityIntent);
    }
}

I'm not sure if the problem has to do with the IntentService or the ActivityRecognitionAPI.

EDIT: Okay, so I let the app run for a bit longer an I am actually receiving more than one update, but it seems completely random as to when I get them. I received 3 in like 10 minutes.

EDIT 2: I tried shaking my phone for like 30 seconds, and now I'm receiving updates every 5 seconds again. I have no idea what is going on.

Jesper
  • 2,644
  • 4
  • 30
  • 65
  • I do not see your intent service receiving intents. I only see you starting a new intent service now and then. – greenapps Oct 21 '17 at 20:47
  • @greenapps But it is receiving intents, just not as often as it's supposed to be. This line is supposed to send intents every 5 seconds: `ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates( mApiClient, 5000, pi);` – Jesper Oct 21 '17 at 21:34
  • You may refer with this [thread](https://stackoverflow.com/questions/23225897/intentservice-not-getting-intents-from-manifest-registration) which suggested to have a short manifest-registered `BroadcastReceiver` that turns around and calls `startService()` to have the service process the broadcasts. You may also check this related [SO post](https://stackoverflow.com/questions/5691104/intentservice-not-receiving-network-state-change-intents). – abielita Oct 22 '17 at 14:58

0 Answers0