Java Code Examples for com.google.android.gms.location.ActivityRecognitionResult#getProbableActivities()

The following examples show how to use com.google.android.gms.location.ActivityRecognitionResult#getProbableActivities() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ActivityRecognitionLocationProvider.java    From background-geolocation-android with Apache License 2.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
    ArrayList<DetectedActivity> detectedActivities = (ArrayList) result.getProbableActivities();

    //Find the activity with the highest percentage
    lastActivity = getProbableActivity(detectedActivities);

    logger.debug("Detected activity={} confidence={}", BackgroundActivity.getActivityString(lastActivity.getType()), lastActivity.getConfidence());

    handleActivity(lastActivity);

    if (lastActivity.getType() == DetectedActivity.STILL) {
        showDebugToast("Detected STILL Activity");
        // stopTracking();
        // we will delay stop tracking after position is found
    } else {
        showDebugToast("Detected ACTIVE Activity");
        startTracking();
    }
    //else do nothing
}
 
Example 2
Source File: BackgroundLocationUpdateService.java    From cordova-background-geolocation-services with Apache License 2.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
  ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
  ArrayList<DetectedActivity> detectedActivities = (ArrayList) result.getProbableActivities();

  //Find the activity with the highest percentage
  lastActivity = Constants.getProbableActivity(detectedActivities);

  Log.w(TAG, "MOST LIKELY ACTIVITY: " + Constants.getActivityString(lastActivity.getType()) + " " + lastActivity.getConfidence());

  Intent mIntent = new Intent(Constants.CALLBACK_ACTIVITY_UPDATE);
  mIntent.putExtra(Constants.ACTIVITY_EXTRA, detectedActivities);
  getApplicationContext().sendBroadcast(mIntent);
  Log.w(TAG, "Activity is recording" + isRecording);

  if(lastActivity.getType() == DetectedActivity.STILL && isRecording) {
      showDebugToast(context, "Detected Activity was STILL, Stop recording");
      stopRecording();
  } else if(lastActivity.getType() != DetectedActivity.STILL && !isRecording) {
      showDebugToast(context, "Detected Activity was ACTIVE, Start Recording");
      startRecording();
  }
  //else do nothing
}
 
Example 3
Source File: DetectedActivitiesIntentService.java    From location-samples with Apache License 2.0 6 votes vote down vote up
/**
 * Handles incoming intents.
 * @param intent The Intent is provided (inside a PendingIntent) when requestActivityUpdates()
 *               is called.
 */
@SuppressWarnings("unchecked")
@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();

    PreferenceManager.getDefaultSharedPreferences(this)
            .edit()
            .putString(Constants.KEY_DETECTED_ACTIVITIES,
                    Utils.detectedActivitiesToJson(detectedActivities))
            .apply();

    // Log each activity.
    Log.i(TAG, "activities detected");
    for (DetectedActivity da: detectedActivities) {
        Log.i(TAG, Utils.getActivityString(
                        getApplicationContext(),
                        da.getType()) + " " + da.getConfidence() + "%"
        );
    }
}
 
Example 4
Source File: SKActivity.java    From SensingKit-Android with GNU Lesser General Public License v3.0 6 votes vote down vote up
private DetectedActivity getActivity(ActivityRecognitionResult result) {

        // Get the most probable activity from the list of activities in the result
        DetectedActivity mostProbableActivity = result.getMostProbableActivity();

        // If the activity is ON_FOOT, choose between WALKING or RUNNING
        if (mostProbableActivity.getType() == DetectedActivity.ON_FOOT) {

            // Iterate through all possible activities. The activities are sorted by most probable activity first.
            for (DetectedActivity activity : result.getProbableActivities()) {

                if (activity.getType() == DetectedActivity.WALKING || activity.getType() == DetectedActivity.RUNNING) {
                    return activity;
                }
            }

            // It is ON_FOOT, but not sure if it is WALKING or RUNNING
            Log.i(TAG, "Activity ON_FOOT, but not sure if it is WALKING or RUNNING.");
            return mostProbableActivity;
        }
        else
        {
            return mostProbableActivity;
        }

    }
 
Example 5
Source File: ActivityRecognitionIntentService.java    From android-notification-log with MIT License 5 votes vote down vote up
@Override
protected void onHandleIntent(@Nullable Intent intent) {
	if(intent == null) {
		return;
	}

	ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);

	ArrayList<DetectedActivity> detectedActivities = (ArrayList<DetectedActivity>) result.getProbableActivities();

	ArrayList<String> activities = new ArrayList<>();
	ArrayList<Integer> confidences = new ArrayList<>();

	for(DetectedActivity activity : detectedActivities) {
		activities.add(getActivityString(activity.getType()));
		confidences.add(activity.getConfidence());
	}

	JSONObject json = new JSONObject();
	String str = null;
	try {
		long now = System.currentTimeMillis();
		json.put("time", now);
		json.put("offset", TimeZone.getDefault().getOffset(now));
		json.put("activities", new JSONArray(activities));
		json.put("confidences", new JSONArray(confidences));
		str = json.toString();
	} catch (JSONException e) {
		if(Const.DEBUG) e.printStackTrace();
	}

	SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
	sp.edit().putString(Const.PREF_LAST_ACTIVITY, str).apply();
}
 
Example 6
Source File: HARecognizerApiIntentService.java    From DataLogger with MIT License 5 votes vote down vote up
@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();

    // Broadcast the list of detected activities.
    LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(Constants.BROADCAST_ACTION)
            .putExtra(Constants.DETECTED_ACTIVITIES_INTENT_KEY, detectedActivities)
    );

}
 
Example 7
Source File: AwarenessImpl.java    From Myna with Apache License 2.0 5 votes vote down vote up
private void handleResult(@NonNull DetectedActivityResult detectedActivityResult){
    ActivityRecognitionResult ar = detectedActivityResult.getActivityRecognitionResult();
    RecognizedActivityResult result = new RecognizedActivityResult();
    List<DetectedActivity> acts = ar.getProbableActivities();
    result.activities = new RecognizedActivity[acts.size()];
    for(int i = 0; i < acts.size(); ++i){
        DetectedActivity act = acts.get(i);
        result.activities[i] = new RecognizedActivity(act.getType(), act.getConfidence());
    }
    resultCallback.onResult(result);
}
 
Example 8
Source File: DetectionService.java    From react-native-activity-recognition with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
    ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent);
    ArrayList<DetectedActivity> detectedActivities = (ArrayList) result.getProbableActivities();

    Log.d(TAG, "Detected activities:");
    for (DetectedActivity da: detectedActivities) {
        Log.d(TAG, getActivityString(da.getType()) + " (" + da.getConfidence() + "%)");
    }

    Intent localIntent = new Intent(BROADCAST_ACTION);
    localIntent.putExtra(ACTIVITY_EXTRA, detectedActivities);
    LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
}
 
Example 9
Source File: ActivityRecognitionIntentService.java    From JayPS-AndroidApp with MIT License 5 votes vote down vote up
private void logActivity(ActivityRecognitionResult result) {
    Log.d(TAG, "Unknown Activity");
    Log.d(TAG, "Probable Activities");
    for(DetectedActivity activity : result.getProbableActivities()) {
        Log.d(TAG, "Most Probable list: " + result.getMostProbableActivity().getType());
        Log.d(TAG, "Most Probable list: " + result.getMostProbableActivity().toString());
    }
    Log.d(TAG, "Most Probable: " + result.getMostProbableActivity().getType());
    Log.d(TAG, "Most Probable: " + result.getMostProbableActivity().toString());
}