Code example for LocationClient

Methods: addGeofencesrequestLocationUpdates

0
        super.onDestroy(); 
    } 
 
    @Override 
    public void onConnected(Bundle bundle) {
        locationClient.requestLocationUpdates(currentlocationRequest, this);
        if(pendingIntentForGeofence != null){
            registerGeofence(pendingIntentForGeofence);
            pendingIntentForGeofence = null;
        } 
    } 
 
    @Override 
    public void onDisconnected() { 
        // nothing to do 
    } 
 
    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) {
        if (connectionResult.hasResolution()) {
            try { 
                // Start an Activity that tries to resolve the error 
                connectionResult.startResolutionForResult(getActivity(), LOCATION_CONNECTION_FAILURE_REQUEST);
                /* 
                 * Thrown if Google Play services canceled the original 
                 * PendingIntent 
                 */ 
            } catch (IntentSender.SendIntentException e) {
                Toast.makeText(getActivity(), "Error code received : " +e.getMessage(), Toast.LENGTH_LONG).show();
            } 
        } else { 
            Toast.makeText(getActivity(), "Error code received : " + connectionResult.getErrorCode(), Toast.LENGTH_LONG).show();
        } 
    } 
 
    @Override 
    public void onLocationChanged(Location location) {
        if(getActivity() instanceof LocationListener) {
            ((LocationListener) getActivity()).onLocationChanged(location);
        } 
    } 
 
 
    /** 
     * Attach a {@link com.google.android.gms.plus.PlusClient} managing fragment to you activity. 
     * 
     * @param activity          The activity to attach the fragment to. 
     * @return The fragment managing a {@link com.google.android.gms.plus.PlusClient}. 
     */ 
    public static LocationFragment getLocationFragment(FragmentActivity activity) {
 
        // Check if the fragment is already attached. 
        FragmentManager fragmentManager = activity.getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentByTag(TAG_LOCATION);
        if (fragment instanceof LocationFragment) {
            // The fragment is attached. 
                return (LocationFragment) fragment;
        } 
 
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        // If a fragment was already attached, remove it to clean up. 
        if (fragment != null) {
            fragmentTransaction.remove(fragment);
        } 
 
        // Create a new fragment and attach it to the fragment manager. 
        LocationFragment locationFragment = new LocationFragment();
        fragmentTransaction.add(locationFragment, TAG_LOCATION);
        fragmentTransaction.commit();
        return locationFragment;
    } 
 
    public void requestGeofence(PendingIntent pendingIntent) {
        if(locationClient == null || !locationClient.isConnected()) {
            pendingIntentForGeofence = pendingIntent;
        } else { 
            registerGeofence(pendingIntent);
        } 
    } 
 
    private void registerGeofence(PendingIntent pendingIntent) {
        Geofence geofence = new Geofence.Builder()
                .setRequestId("test")
                .setCircularRegion(48.879593, 2.415551, 100)
                .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER | Geofence.GEOFENCE_TRANSITION_EXIT)
                .setExpirationDuration(Geofence.NEVER_EXPIRE)
                .build();
        locationClient.addGeofences(Arrays.asList(geofence), pendingIntent, this);
    } 
 
    @Override 
    public void onAddGeofencesResult(int i, String[] strings) {
    } 
Stop searching for code, let great code find you!  Add Codota to your java IDE