Java 类com.google.android.gms.location.ActivityRecognition 实例源码

项目:Saiy-PS    文件:MotionRecognition.java   
/**
 * Cancel any future callbacks and disconnect the client.
 */
public void destroy() {

    if (activityClient != null && pendingIntent != null) {

        try {
            ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(activityClient, pendingIntent);
            activityClient.disconnect();
        } catch (final Exception e) {
            if (DEBUG) {
                MyLog.i(CLS_NAME, "destroy: Exception");
                e.printStackTrace();
            }
        }
    }
}
项目:cordova-plugin-quintech-background-geolocation    文件:ActivityRecognitionLocationProvider.java   
private void attachRecorder() {
    if (googleApiClient == null) {
        connectToPlayAPI();
    } else if (googleApiClient.isConnected()) {
        if (isWatchingActivity) { return; }
        startTracking();
        if (config.getStopOnStillActivity()) {
            ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
                googleApiClient,
                config.getActivitiesInterval(),
                detectedActivitiesPI
            );
            isWatchingActivity = true;
        }
    } else {
        googleApiClient.connect();
    }
}
项目:xDrip    文件:ActivityRecognizedService.java   
public synchronized void start(boolean no_rate_limit) {
    if (Pref.getBoolean("use_remote_motion", false)) {
        Log.d(TAG, "Not starting as we are expecting remote instead of local motion");
        return;
    }
    if ((no_rate_limit) || (JoH.ratelimit("recognizer-start", 60))) {
        release_wl_start();
        wl_start = JoH.getWakeLock("recognizer-start", 60000);
        UserError.Log.e(TAG, "Restarting API");
        mApiClient = new GoogleApiClient.Builder(this)
                .addApi(ActivityRecognition.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        mApiClient.connect();
    } else {
        UserError.Log.e(TAG, "Couldn't restart API due to ratelimit");
    }
}
项目:xDrip-plus    文件:ActivityRecognizedService.java   
public synchronized void start(boolean no_rate_limit) {
    if (Pref.getBoolean("use_remote_motion", false)) {
        Log.d(TAG, "Not starting as we are expecting remote instead of local motion");
        return;
    }
    if ((no_rate_limit) || (JoH.ratelimit("recognizer-start", 60))) {
        release_wl_start();
        wl_start = JoH.getWakeLock("recognizer-start", 60000);
        UserError.Log.e(TAG, "Restarting API");
        mApiClient = new GoogleApiClient.Builder(this)
                .addApi(ActivityRecognition.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

        mApiClient.connect();
    } else {
        UserError.Log.e(TAG, "Couldn't restart API due to ratelimit");
    }
}
项目:assistance-platform-client-sdk-android    文件:MotionActivitySensor.java   
@Override
public void stopSensor() {

    if (mGoogleApiClient != null) {
        try {
            if (mGoogleApiClient.isConnected()) {

                ActivityRecognition.ActivityRecognitionApi
                        .removeActivityUpdates(
                                mGoogleApiClient,
                                mActivityRecognitionPendingIntent);

                mGoogleApiClient.disconnect();
            }
        } catch (IllegalStateException e) {
            Log.e(TAG, "Cannot disconnect from Google Api!", e);
        } finally {
            setRunning(false);
            mGoogleApiClient = null;
        }
    }
}
项目:react-native-activity-recognition    文件:ActivityRecognizer.java   
public ActivityRecognizer(ReactApplicationContext reactContext) {
    mGoogleApiAvailability = GoogleApiAvailability.getInstance();
    mContext = reactContext.getApplicationContext();
    mReactContext = reactContext;
    connected = false;
    started = false;

    if (checkPlayServices()) {
        mBroadcastReceiver = new ActivityDetectionBroadcastReceiver();
        mGoogleApiClient = new GoogleApiClient.Builder(mContext)
            .addApi(ActivityRecognition.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    }
}
项目:react-native-activity-recognition    文件:ActivityRecognizer.java   
public void start(long detectionIntervalMillis) {
    if (mGoogleApiClient == null) {
        throw new Error("No Google API client. Your device likely doesn't have Google Play Services.");
    }

    interval = detectionIntervalMillis;
    if (!connected) {
        mGoogleApiClient.connect();
        LocalBroadcastManager.getInstance(mContext).registerReceiver(mBroadcastReceiver, new IntentFilter(DetectionService.BROADCAST_ACTION));
    } else if (!started) {
        ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
            mGoogleApiClient,
            detectionIntervalMillis,
            getActivityDetectionPendingIntent()
        ).setResultCallback(this);
        started = true;
    }
}
项目:react-native-activity-recognition    文件:ActivityRecognizer.java   
public void stop() {
    if (mGoogleApiClient == null) {
        throw new Error("No Google API client. Your device likely doesn't have Google Play Services.");
    }

    if (started) {
        ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(
            mGoogleApiClient,
            getActivityDetectionPendingIntent()
        ).setResultCallback(this);
        started = false;
    }
    if (connected) {
        LocalBroadcastManager.getInstance(mContext).unregisterReceiver(mBroadcastReceiver);
        mGoogleApiClient.disconnect();
        connected = false;
    }
}
项目:ActivityMapper    文件:LocationManager.java   
public LocationManager(Context context) {
    _locationCache = new ArrayList<>();
    _detectedActivitiesCache = new ArrayList<>();

    _googleApiClient = new GoogleApiClient.Builder(context)
            .addApi(LocationServices.API)
            .addApi(ActivityRecognition.API)
            .addConnectionCallbacks(new GoogleApiConnectionCallbacks())
            .build();

    _fusedLocationListener = new FusedLocationListener();
    Intent intent = new Intent(context, DetectedActivitiesIntentService.class);

    // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
    // requestActivityUpdates() and removeActivityUpdates().
    _activityPendingIntent = PendingIntent.getService(context,
                                                      0,
                                                      intent,
                                                      PendingIntent.FLAG_UPDATE_CURRENT);
    connect();
}
项目:BackPackTrackII    文件:BackgroundService.java   
private static void startActivityRecognition(final Context context) {
    if (Util.hasPlayServices(context)) {
        GoogleApiClient gac = new GoogleApiClient.Builder(context).addApi(ActivityRecognition.API).build();
        if (gac.blockingConnect().isSuccess()) {
            Log.i(TAG, "GoogleApiClient connected");
            Intent activityIntent = new Intent(context, BackgroundService.class);
            activityIntent.setAction(BackgroundService.ACTION_ACTIVITY);
            PendingIntent pi = PendingIntent.getService(context, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
            boolean still = (prefs.getInt(SettingsFragment.PREF_LAST_ACTIVITY, DetectedActivity.STILL) == DetectedActivity.STILL);
            String setting = (still ? SettingsFragment.PREF_RECOGNITION_INTERVAL_STILL : SettingsFragment.PREF_RECOGNITION_INTERVAL_MOVING);
            String standard = (still ? SettingsFragment.DEFAULT_RECOGNITION_INTERVAL_STILL : SettingsFragment.DEFAULT_RECOGNITION_INTERVAL_MOVING);
            int interval = Integer.parseInt(prefs.getString(setting, standard));

            ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(gac, interval * 1000, pi);
            Log.i(TAG, "Activity updates frequency=" + interval + "s");
        }
    }
}
项目:BackPackTrackII    文件:BackgroundService.java   
private static void stopActivityRecognition(final Context context) {
    if (Util.hasPlayServices(context)) {
        GoogleApiClient gac = new GoogleApiClient.Builder(context).addApi(ActivityRecognition.API).build();
        if (gac.blockingConnect().isSuccess()) {
            Log.i(TAG, "GoogleApiClient connected");
            Intent activityIntent = new Intent(context, BackgroundService.class);
            activityIntent.setAction(BackgroundService.ACTION_ACTIVITY);
            PendingIntent pi = PendingIntent.getService(context, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(gac, pi);

            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
            SharedPreferences.Editor editor = prefs.edit();
            editor.remove(SettingsFragment.PREF_LAST_ACTIVITY);
            editor.remove(SettingsFragment.PREF_LAST_CONFIDENCE);
            editor.apply();
            Log.i(TAG, "Canceled activity updates");
        }
    }
}
项目:cordova-background-geolocation-services    文件:BackgroundLocationUpdateService.java   
private void attachDARecorder() {
  if (detectedActivitiesAPI == null) {
      buildDAClient();
  } else if (detectedActivitiesAPI.isConnected()) {
    ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
                    detectedActivitiesAPI,
                    this.activitiesInterval,
                    detectedActivitiesPI
            );
      if(isDebugging) {
          Log.d(TAG, "- DA RECORDER attached - start recording location updates");
      }
  } else {
    Log.i(TAG, "NOT CONNECTED, CONNECT");
      detectedActivitiesAPI.connect();
  }
}
项目:HereAStory-Android    文件:GameService.java   
public void registerActivityUpdates() {
    if (mRecognitionPendingIntent == null) {
        mRecognitionPendingIntent = PendingIntent.getService(this, 0,
                new Intent(this, RecognitionIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT);
    }
    ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mClient,
            ACTIVITY_INTERVAL,
            mRecognitionPendingIntent).setResultCallback(new ResultCallback<Status>() {
        @Override
        public void onResult(Status status) {
            if(status.isSuccess()) {
                Log.d(TAG, "Activities coming!");
            } else {
                Log.e(TAG, "No activity coming!");
            }
        }
    });
}
项目:GitHub    文件:ActivityUpdatesObservable.java   
@Override
protected void onGoogleApiClientReady(GoogleApiClient apiClient, Observer<? super ActivityRecognitionResult> observer) {
    receiver = new ActivityUpdatesBroadcastReceiver(observer);
    context.registerReceiver(receiver, new IntentFilter(ACTION_ACTIVITY_DETECTED));
    PendingIntent receiverIntent = getReceiverPendingIntent();
    ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(apiClient, detectionIntervalMilliseconds, receiverIntent);
}
项目:GitHub    文件:ActivityUpdatesObservable.java   
@Override
protected void onUnsubscribed(GoogleApiClient apiClient) {
    if (apiClient.isConnected()) {
        ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(apiClient, getReceiverPendingIntent());
    }
    context.unregisterReceiver(receiver);
    receiver = null;
}
项目:RxGps    文件:RxLocationBaseOnSubscribe.java   
protected RxLocationBaseOnSubscribe(@NonNull RxLocation rxLocation, Long timeout, TimeUnit timeUnit) {
    this.ctx = rxLocation.ctx;
    this.services = new Api[]{ LocationServices.API, ActivityRecognition.API };
    this.scopes = null;

    if (timeout != null && timeUnit != null) {
        this.timeoutTime = timeout;
        this.timeoutUnit = timeUnit;
    } else {
        this.timeoutTime = rxLocation.timeoutTime;
        this.timeoutUnit = rxLocation.timeoutUnit;
    }
}
项目:RxGps    文件:ActivityRequestUpdatesSingleOnSubscribe.java   
@Override
protected void onGoogleApiClientReady(GoogleApiClient apiClient, SingleEmitter<Status> emitter) {
    //noinspection MissingPermission
    setupLocationPendingResult(
            ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(apiClient, detectionIntervalMillis, pendingIntent),
            SingleResultCallBack.get(emitter)
    );
}
项目:RxGps    文件:ActivityRemoveUpdatesSingleOnSubscribe.java   
@Override
protected void onGoogleApiClientReady(GoogleApiClient apiClient, SingleEmitter<Status> emitter) {
    //noinspection MissingPermission
    setupLocationPendingResult(
            ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(apiClient, pendingIntent),
            SingleResultCallBack.get(emitter)
    );
}
项目:Saiy-PS    文件:MotionRecognition.java   
@Override
public void onConnected(@Nullable final Bundle bundle) {
    if (DEBUG) {
        MyLog.i(CLS_NAME, "onConnected");
    }

    if (activityClient != null && pendingIntent != null) {
        ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(activityClient,
                MotionIntentService.UPDATE_INTERVAL, pendingIntent).setResultCallback(this);
    }
}
项目:cordova-plugin-quintech-background-geolocation    文件:ActivityRecognitionLocationProvider.java   
private void connectToPlayAPI() {
    Log.d(TAG, "connecting to Google Play Services");
    googleApiClient =  new GoogleApiClient.Builder(context)
            .addApi(LocationServices.API)
            .addApi(ActivityRecognition.API)
            .addConnectionCallbacks(this)
            //.addOnConnectionFailedListener(this)
            .build();
    googleApiClient.connect();
}
项目:cordova-plugin-quintech-background-geolocation    文件:ActivityRecognitionLocationProvider.java   
private void detachRecorder() {
    if (isWatchingActivity) {
        Log.d(TAG, "detachRecorder");
        ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(googleApiClient, detectedActivitiesPI);
        isWatchingActivity = false;
    }
}
项目:xDrip    文件:ActivityRecognizedService.java   
private synchronized void requestUpdates(int frequency) {
    try {
        received = 0; // reset
        wl_global = JoH.getWakeLock("motion-wait", frequency * 5); // released later
        if (d) Log.d(TAG, "requestUpdates called: " + frequency);
        incrementInternalPrefsLong(REQUESTED);
        interpretRatio(this);
        ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mApiClient, frequency, get_pending_intent());
    } catch (Exception e) {
        UserError.Log.wtf(TAG, "Got exception starting activity recognition: " + e.toString());
    }
}
项目:xDrip    文件:ActivityRecognizedService.java   
private void stopUpdates() {
    try {
        if (d) Log.d(TAG, "stopUpdates called");
        ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(mApiClient, get_pending_intent());
        if (wl_global != null) {
            if (d) Log.d(TAG, "release wl_global");
            JoH.releaseWakeLock(wl_global);
            wl_global = null;
        }
    } catch (Exception e) {
        UserError.Log.wtf(TAG, "Got exception stopping activity recognition: " + e.toString());
    }
}
项目:xDrip-plus    文件:ActivityRecognizedService.java   
private synchronized void requestUpdates(int frequency) {
    try {
        received = 0; // reset
        wl_global = JoH.getWakeLock("motion-wait", frequency * 5); // released later
        if (d) Log.d(TAG, "requestUpdates called: " + frequency);
        incrementInternalPrefsLong(REQUESTED);
        interpretRatio(this);
        ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(mApiClient, frequency, get_pending_intent());
    } catch (Exception e) {
        UserError.Log.wtf(TAG, "Got exception starting activity recognition: " + e.toString());
    }
}
项目:xDrip-plus    文件:ActivityRecognizedService.java   
private void stopUpdates() {
    try {
        if (d) Log.d(TAG, "stopUpdates called");
        ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(mApiClient, get_pending_intent());
        if (wl_global != null) {
            if (d) Log.d(TAG, "release wl_global");
            JoH.releaseWakeLock(wl_global);
            wl_global = null;
        }
    } catch (Exception e) {
        UserError.Log.wtf(TAG, "Got exception stopping activity recognition: " + e.toString());
    }
}
项目:patrol-android    文件:PlayServicesLocationApi.java   
protected synchronized void buildGoogleApiClient(Context context) {
    mGoogleApiClient = new GoogleApiClient.Builder(context)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .addApi(ActivityRecognition.API)
            .build();
    mGoogleApiClient.connect();
}
项目:Activity-Recognition-Tools    文件:MainActivity.java   
/**
 * Builds a GoogleApiClient. Uses the {@code #addApi} method to request the
 * ActivityRecognition API.
 */
protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(ActivityRecognition.API)
            .build();
}
项目:Activity-Recognition-Tools    文件:MainActivity.java   
/**
 * Registers for activity recognition updates using
 * {@link com.google.android.gms.location.ActivityRecognitionApi#requestActivityUpdates} which
 * returns a {@link com.google.android.gms.common.api.PendingResult}. Since this activity
 * implements the PendingResult interface, the activity itself receives the callback, and the
 * code within {@code onResult} executes. Note: once {@code requestActivityUpdates()} completes
 * successfully, the {@code DetectedActivitiesIntentService} starts receiving callbacks when
 * activities are detected.
 */
public void requestActivityUpdatesButtonHandler(View view) {
    if (!mGoogleApiClient.isConnected()) {
        Toast.makeText(this, getString(R.string.not_connected),
                Toast.LENGTH_SHORT).show();
        return;
    }
    ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
            mGoogleApiClient,
            Constants.DETECTION_INTERVAL_IN_MILLISECONDS,
            getActivityDetectionPendingIntent()
    ).setResultCallback(this);
}
项目:Activity-Recognition-Tools    文件:MainActivity.java   
/**
 * Removes activity recognition updates using
 * {@link com.google.android.gms.location.ActivityRecognitionApi#removeActivityUpdates} which
 * returns a {@link com.google.android.gms.common.api.PendingResult}. Since this activity
 * implements the PendingResult interface, the activity itself receives the callback, and the
 * code within {@code onResult} executes. Note: once {@code removeActivityUpdates()} completes
 * successfully, the {@code DetectedActivitiesIntentService} stops receiving callbacks about
 * detected activities.
 */
public void removeActivityUpdatesButtonHandler(View view) {
    if (!mGoogleApiClient.isConnected()) {
        Toast.makeText(this, getString(R.string.not_connected), Toast.LENGTH_SHORT).show();
        return;
    }
    // Remove all activity updates for the PendingIntent that was used to request activity
    // updates.
    ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(
            mGoogleApiClient,
            getActivityDetectionPendingIntent()
    ).setResultCallback(this);
}
项目:assistance-platform-client-sdk-android    文件:MotionActivitySensor.java   
@NonNull
public GoogleApiClient getGoogleApiClient() {

    return new Builder(context)
            .addApi(ActivityRecognition.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
}
项目:gps-measurement-tools    文件:MainActivity.java   
private synchronized void buildGoogleApiClient() {
  mGoogleApiClient =
      new GoogleApiClient.Builder(this)
          .enableAutoManage(this, this)
          .addConnectionCallbacks(this)
          .addOnConnectionFailedListener(this)
          .addApi(ActivityRecognition.API)
          .build();
}
项目:gps-measurement-tools    文件:MainActivity.java   
@Override
public void onConnected(Bundle connectionHint) {
  if (Log.isLoggable(TAG, Log.INFO)) {
    Log.i(TAG, "Connected to GoogleApiClient");
  }
  ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
      mGoogleApiClient, 0, createActivityDetectionPendingIntent());
}
项目:CoachTracker    文件:ActivityRecognizerImpl.java   
public ActivityRecognizerImpl(Context context) {
    this.context = context;
    googleApiClient = new GoogleApiClient.Builder(context)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(ActivityRecognition.API)
            .build();
}
项目:CoachTracker    文件:ActivityRecognizerImpl.java   
@Override
public void stopToRecognizeActivities() {
    if (googleApiClient.isConnected()) {
        ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(
                googleApiClient,
                PendingIntent.getService(context, 0,
                        new Intent(context, ActivityRecognitionIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT)
        ); //Stopping to recognize activities.
        googleApiClient.disconnect(); //Closing connection with Google APIs
    }
}
项目:CoachTracker    文件:ActivityRecognizerImpl.java   
/**
 * After calling connect(), this method will be invoked asynchronously when the connect request has successfully completed.
 * After this callback, the application can make requests on other methods provided by the client and expect that no user intervention
 * is required to call methods that use account and scopes provided to the client constructor.
 *
 * @param bundle
 */
@Override
public void onConnected(Bundle bundle) {
    //Google API connection has been done successfully, now we can work with it
    ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
            googleApiClient,
            ACTIVITY_RECOGNITION_INTERVAL,
            PendingIntent.getService(context, 0,
                    new Intent(context, ActivityRecognitionIntentService.class), PendingIntent.FLAG_UPDATE_CURRENT)
    );
}
项目:activity-recognition-example    文件:GClient.java   
protected synchronized void buildGoogleApiClient() {
    Timber.d("Building the Google Api Client");
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(ActivityRecognition.API)
            .build();
}
项目:activity-recognition-example    文件:GClient.java   
@Override
public void onDestroy() {
    Timber.d("Service destroyed, stopping the google client");
    ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(
            mGoogleApiClient,
            getActivityDetectionPendingIntent()
    ).setResultCallback(this);
    mGoogleApiClient.disconnect();
}
项目:activity-recognition-example    文件:GClient.java   
@Override
public void onConnected(Bundle connectionHint) {
    Timber.d("Google Client connected");
    ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
            mGoogleApiClient,
            Constants.DETECTION_INTERVAL_IN_MILLISECONDS,
            getActivityDetectionPendingIntent()
    ).setResultCallback(this);
}
项目:cordova-background-geolocation-services    文件:BackgroundLocationUpdateService.java   
private void detatchDARecorder() {
  if (detectedActivitiesAPI == null) {
      buildDAClient();
  } else if (detectedActivitiesAPI.isConnected()) {
    ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(detectedActivitiesAPI, detectedActivitiesPI);
      if(isDebugging) {
          Log.d(TAG, "- Recorder detached - stop recording activity updates");
      }
  } else {
      detectedActivitiesAPI.connect();
  }
}
项目:cordova-background-geolocation-services    文件:BackgroundLocationUpdateService.java   
@Override
public void onConnected(Bundle bundle) {
    Log.w(TAG, "Activity Client Connected");
    ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(
            detectedActivitiesAPI,
            activitiesInterval,
            detectedActivitiesPI
    );
}