com.google.android.gms.location.ActivityRecognitionClient Java Examples

The following examples show how to use com.google.android.gms.location.ActivityRecognitionClient. 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: MainActivity.java    From location-samples with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    mContext = this;

    // Get the UI widgets.
    mRequestActivityUpdatesButton = (Button) findViewById(R.id.request_activity_updates_button);
    mRemoveActivityUpdatesButton = (Button) findViewById(R.id.remove_activity_updates_button);
    ListView detectedActivitiesListView = (ListView) findViewById(
            R.id.detected_activities_listview);

    // Enable either the Request Updates button or the Remove Updates button depending on
    // whether activity updates have been requested.
    setButtonsEnabledState();

    ArrayList<DetectedActivity> detectedActivities = Utils.detectedActivitiesFromJson(
            PreferenceManager.getDefaultSharedPreferences(this).getString(
                    Constants.KEY_DETECTED_ACTIVITIES, ""));

    // Bind the adapter to the ListView responsible for display data for detected activities.
    mAdapter = new DetectedActivitiesAdapter(this, detectedActivities);
    detectedActivitiesListView.setAdapter(mAdapter);

    mActivityRecognitionClient = new ActivityRecognitionClient(this);
}
 
Example #2
Source File: TrackRecordingService.java    From mytracks with Apache License 2.0 4 votes vote down vote up
@Override
public void onCreate() {
  super.onCreate();
  executorService = Executors.newSingleThreadExecutor();
  context = this;
  myTracksProviderUtils = MyTracksProviderUtils.Factory.get(this);
  handler = new Handler();
  myTracksLocationManager = new MyTracksLocationManager(this, handler.getLooper(), true);
  activityRecognitionPendingIntent = PendingIntent.getService(context, 0,
      new Intent(context, ActivityRecognitionIntentService.class),
      PendingIntent.FLAG_UPDATE_CURRENT);
  activityRecognitionClient = new ActivityRecognitionClient(
      context, activityRecognitionCallbacks, activityRecognitionFailedListener);
  activityRecognitionClient.connect();    
  voiceExecutor = new PeriodicTaskExecutor(this, new AnnouncementPeriodicTaskFactory());
  splitExecutor = new PeriodicTaskExecutor(this, new SplitPeriodicTaskFactory());
  sharedPreferences = getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
  sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);

  // onSharedPreferenceChanged might not set recordingTrackId.
  recordingTrackId = PreferencesUtils.RECORDING_TRACK_ID_DEFAULT;

  // Require voiceExecutor and splitExecutor to be created.
  sharedPreferenceChangeListener.onSharedPreferenceChanged(sharedPreferences, null);
  
  handler.post(registerLocationRunnable);
  
  /*
   * Try to restart the previous recording track in case the service has been
   * restarted by the system, which can sometimes happen.
   */
  Track track = myTracksProviderUtils.getTrack(recordingTrackId);
  if (track != null) {
    restartTrack(track);
  } else {
    if (isRecording()) {
      Log.w(TAG, "track is null, but recordingTrackId not -1L. " + recordingTrackId);
      updateRecordingState(PreferencesUtils.RECORDING_TRACK_ID_DEFAULT, true);
    }
    showNotification(false);
  }
}
 
Example #3
Source File: BackgroundService.java    From GeoLog with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
	Debug.log("Thread init");
				
	databaseHelper = Database.Helper.getInstance(context);
	
	alarm = (AlarmManager)context.getSystemService(ALARM_SERVICE);
	{
		Intent i = new Intent(context.getApplicationContext(), BackgroundService.class);
		i.putExtra(EXTRA_ALARM_CALLBACK, 1);
		alarmCallback =	PendingIntent.getService(BackgroundService.this, 0, i, 0);
	}

	Looper.prepare();
	handler = new Handler();
			
	Debug.log("Registering for updates");			
	prefs = PreferenceManager.getDefaultSharedPreferences(context);					
	long id = prefs.getLong(SettingsFragment.PREF_CURRENT_PROFILE, 0);
	if (id > 0) currentProfile = Database.Profile.getById(databaseHelper, id, null);
	if (currentProfile == null) currentProfile = Database.Profile.getOffProfile(databaseHelper);
	metric = !prefs.getString(SettingsFragment.PREF_UNITS, SettingsFragment.PREF_UNITS_DEFAULT).equals(SettingsFragment.VALUE_UNITS_IMPERIAL);
	prefs.registerOnSharedPreferenceChangeListener(preferencesUpdated);
	LocalBroadcastManager.getInstance(context).registerReceiver(databaseUpdated, new IntentFilter(Database.Helper.NOTIFY_BROADCAST));
	
	Debug.log("Registering for power levels");
	context.registerReceiver(batteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
	
	Debug.log("Connecting ActivityRecognitionClient");
	activityIntent = PendingIntent.getService(context, 1, new Intent(context, BackgroundService.class), 0);
	activityClient = new ActivityRecognitionClient(context, activityConnectionCallbacks, activityConnectionFailed);
	activityClient.connect();

	Debug.log("Connecting LocationClient");
	locationClient = new LocationClient(context, locationConnectionCallbacks, locationConnectionFailed);
	locationClient.connect();
	
	Debug.log("Entering loop");
	handler.post(new Runnable() {				
		@Override
		public void run() {
			updateListeners(FLAG_SETUP);
		}
	});						
	Looper.loop();			
	Debug.log("Exiting loop");
	
	context.unregisterReceiver(batteryReceiver);
	
	LocalBroadcastManager.getInstance(context).unregisterReceiver(databaseUpdated);
	prefs.unregisterOnSharedPreferenceChangeListener(preferencesUpdated);			
	
	if (activityConnected) {
		activityClient.removeActivityUpdates(activityIntent);
		activityClient.disconnect();
	}
	if (locationConnected) {
		locationClient.removeLocationUpdates(locationListener);
		locationClient.disconnect();
	}			
}