Here are the examples of the java api class com.google.android.gms.location.DetectedActivity taken from open source projects.
1. DetectedActivitiesIntentService#onHandleIntent()
Project: android-play-location
File: DetectedActivitiesIntentService.java
File: DetectedActivitiesIntentService.java
/** * Handles incoming intents. * @param intent The Intent is provided (inside a PendingIntent) when requestActivityUpdates() * is called. */ @Override protected void onHandleIntent(Intent intent) { ActivityRecognitionResult result = ActivityRecognitionResult.extractResult(intent); Intent localIntent = new Intent(Constants.BROADCAST_ACTION); // 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(); // Log each activity. Log.i(TAG, "activities detected"); for (DetectedActivity da : detectedActivities) { Log.i(TAG, Constants.getActivityString(getApplicationContext(), da.getType()) + " " + da.getConfidence() + "%"); } // Broadcast the list of detected activities. localIntent.putExtra(Constants.ACTIVITY_EXTRA, detectedActivities); LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent); }
2. DetectedActivitiesAdapter#getView()
Project: android-play-location
File: DetectedActivitiesAdapter.java
File: DetectedActivitiesAdapter.java
@Override public View getView(int position, View view, ViewGroup parent) { DetectedActivity detectedActivity = getItem(position); if (view == null) { view = LayoutInflater.from(getContext()).inflate(R.layout.detected_activity, parent, false); } // Find the UI widgets. TextView activityName = (TextView) view.findViewById(R.id.detected_activity_name); TextView activityConfidenceLevel = (TextView) view.findViewById(R.id.detected_activity_confidence_level); ProgressBar progressBar = (ProgressBar) view.findViewById(R.id.detected_activity_progress_bar); // Populate widgets with values. activityName.setText(Constants.getActivityString(getContext(), detectedActivity.getType())); activityConfidenceLevel.setText(detectedActivity.getConfidence() + "%"); progressBar.setProgress(detectedActivity.getConfidence()); return view; }
3. DetectedActivitiesAdapter#updateActivities()
Project: android-play-location
File: DetectedActivitiesAdapter.java
File: DetectedActivitiesAdapter.java
/** * Process list of recently detected activities and updates the list of {@code DetectedActivity} * objects backing this adapter. * * @param detectedActivities the freshly detected activities */ protected void updateActivities(ArrayList<DetectedActivity> detectedActivities) { HashMap<Integer, Integer> detectedActivitiesMap = new HashMap<>(); for (DetectedActivity activity : detectedActivities) { detectedActivitiesMap.put(activity.getType(), activity.getConfidence()); } // Every time we detect new activities, we want to reset the confidence level of ALL // activities that we monitor. Since we cannot directly change the confidence // of a DetectedActivity, we use a temporary list of DetectedActivity objects. If an // activity was freshly detected, we use its confidence level. Otherwise, we set the // confidence level to zero. ArrayList<DetectedActivity> tempList = new ArrayList<DetectedActivity>(); for (int i = 0; i < Constants.MONITORED_ACTIVITIES.length; i++) { int confidence = detectedActivitiesMap.containsKey(Constants.MONITORED_ACTIVITIES[i]) ? detectedActivitiesMap.get(Constants.MONITORED_ACTIVITIES[i]) : 0; tempList.add(new DetectedActivity(Constants.MONITORED_ACTIVITIES[i], confidence)); } // Remove all items. this.clear(); // changed and views reflecting the data should refresh. for (DetectedActivity detectedActivity : tempList) { this.add(detectedActivity); } }
4. ActivityRecognizedService#handleDetectedActivities()
Project: AndroidDemoProjects
File: ActivityRecognizedService.java
File: ActivityRecognizedService.java
private void handleDetectedActivities(List<DetectedActivity> probableActivities) { for (DetectedActivity activity : probableActivities) { switch(activity.getType()) { case DetectedActivity.IN_VEHICLE: { Log.e("ActivityRecogition", "In Vehicle: " + activity.getConfidence()); break; } case DetectedActivity.ON_BICYCLE: { Log.e("ActivityRecogition", "On Bicycle: " + activity.getConfidence()); break; } case DetectedActivity.ON_FOOT: { Log.e("ActivityRecogition", "On Foot: " + activity.getConfidence()); break; } case DetectedActivity.RUNNING: { Log.e("ActivityRecogition", "Running: " + activity.getConfidence()); break; } case DetectedActivity.STILL: { Log.e("ActivityRecogition", "Still: " + activity.getConfidence()); break; } case DetectedActivity.TILTING: { Log.e("ActivityRecogition", "Tilting: " + activity.getConfidence()); break; } case DetectedActivity.WALKING: { Log.e("ActivityRecogition", "Walking: " + activity.getConfidence()); if (activity.getConfidence() >= 75) { NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setContentText("Are you walking?"); builder.setSmallIcon(R.mipmap.ic_launcher); builder.setContentTitle(getString(R.string.app_name)); NotificationManagerCompat.from(this).notify(0, builder.build()); } break; } case DetectedActivity.UNKNOWN: { Log.e("ActivityRecogition", "Unknown: " + activity.getConfidence()); break; } } } }