0

my Android App needs to detect the current Activity (running, walking, ...).

I followed the instructions on the Android Dev Page: http://developer.android.com/reference/com/google/android/gms/location/ActivityRecognitionClient.html

// Connect to the ActivityRecognitionService
ActivityRecognitionClient mActivityRecognitionClient =
       new ActivityRecognitionClient(this, this, this);
mActivityRecognitionClient.connect();

// Called when a connection to the ActivityRecognitionService has been established.
public void onConnected(Bundle connectionHint) {
   Intent intent = new Intent(this, MyIntentService.class);
   PendingIntent callbackIntent = PendingIntent.getService(this, 0, intent,
           PendingIntent.FLAG_UPDATE_CURRENT);
   mActivityRecognitionClient.requestActivityUpdates(30000, callbackIntent);
}

And in my Intent-Service:

protected void onHandleIntent(Intent intent) {
   if (ActivityRecognitionResult.hasResult(intent)) {
       ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
       // Put your application specific logic here (i.e. result.getMostProbableActivity())
   }
}

Here's my problem: I want to start the activity recognition on my own. The Code from the Android Dev Page seems, like it's supposed to do that. BUT if I don't add the service to the AndroidManifest, it won't do anything. The onConnected() method gets called, but not onHandleIntent().

So I thought I might just need to add it to the Manifest. Now I do receive Activities, but I receive them all the time. This is also not what I want. What am I doing wrong?

Thanks in advance, Steff

Stefan
  • 295
  • 3
  • 18

1 Answers1

0

Alternatively, you can do it "manually", without google-play-services. See my answer here. The main idea is to detect current top Activity, which is Activity of external application. You can create Service, which will monitor top activities.

Community
  • 1
  • 1
Veaceslav Gaidarji
  • 4,261
  • 3
  • 38
  • 61
  • Thanks, but I still hope to find a solution with which I can use the logic given by Android. Why would I call `mActivityRecognitionClient.connect();`if there was no chance to start the recognition programatically. If I add the service to the manifest, I don't even call that function and it still works. – Stefan Apr 18 '14 at 06:51