Example usage for android.app Activity unbindService

List of usage examples for android.app Activity unbindService

Introduction

In this page you can find the example usage for android.app Activity unbindService.

Prototype

@Override
    public void unbindService(ServiceConnection conn) 

Source Link

Usage

From source file:com.tenforwardconsulting.cordova.BackgroundGeolocationPlugin.java

void doUnbindService() {
    if (isBound) {
        log.info("Unbinding from bg service");
        // If we have received the service, and hence registered with
        // it, then now is the time to unregister.
        if (mService != null) {
            try {
                Message msg = Message.obtain(null, LocationService.MSG_UNREGISTER_CLIENT);
                msg.replyTo = mMessenger;
                mService.send(msg);/*from   w  ww  .j a v  a  2s .  c o m*/
            } catch (RemoteException e) {
                // There is nothing special we need to do if the service
                // has crashed.
            }

            // Detach our existing connection.
            Activity activity = getActivity();
            activity.unbindService(mConnection);
            isBound = false;
        }
    }
}

From source file:com.zion.htf.ui.fragment.ArtistSoundcloudFragment.java

/**
 * Requests the activity to unbind from the service.
 * @param activity the current fragment's activity, acting as a proxy to the service
 *//*from  w  w w.  ja  va  2 s.c  o m*/
private void requestServiceUnbind(Activity activity) {
    ArtistSoundcloudFragment.MediaPlayerServiceProxy serviceProxy = (ArtistSoundcloudFragment.MediaPlayerServiceProxy) activity;
    AbstractServiceProxyActivity serviceProxyActivity = (AbstractServiceProxyActivity) activity;
    if (serviceProxyActivity.isServiceBound()) {
        activity.unbindService(serviceProxy.getServiceConnection());
    } else {
        Log.w("ArtistSoundcloudFragment", "Useless call");
    }
}

From source file:net.texh.cordovapluginstepcounter.CordovaStepCounter.java

@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
    LOG.i(TAG, "execute()");
    Boolean result = true;/*from www .j ava  2 s.c o  m*/

    Activity activity = this.cordova.getActivity();
    stepCounterIntent = new Intent(activity, StepCounterService.class);

    //Get stored value for pedometerActive
    SharedPreferences sharedPref = activity.getSharedPreferences(USER_DATA_PREF, Context.MODE_PRIVATE);
    Boolean pActive = this.getPedometerIsActive(sharedPref);

    if (ACTION_CAN_COUNT_STEPS.equals(action)) {
        Boolean can = deviceHasStepCounter(activity.getPackageManager());
        Log.i(TAG, "Checking if device has step counter APIS: " + can);
        callbackContext.success(can ? 1 : 0);
    } else if (ACTION_START.equals(action)) {

        if (!pActive) {
            Log.i(TAG, "Starting StepCounterService");
            //Update pedometerActive preference
            this.setPedometerIsActive(sharedPref, true);
            activity.startService(stepCounterIntent);
        } else {
            Log.i(TAG, "StepCounterService Already Started before, just binding to it");
        }

        if (!bound) {
            Log.i(TAG, "Binding StepCounterService");
            activity.bindService(stepCounterIntent, mConnection, Context.BIND_AUTO_CREATE);
        } else {
            Log.i(TAG, "StepCounterService already binded");
        }

        //Try getting given param (starting value)
        Integer startingValue = 0;
        try {
            if (data.length() > 0) {
                startingValue = data.getInt(0);
            }
        } catch (JSONException error) {
            Log.i(TAG, "JSON EXCEPTION While Reading startingValue from 'execute' parameters (JSONArray)");
        }

        //Reset TOTAL COUNT on start
        CordovaStepCounter.setTotalCount(sharedPref, startingValue);
        callbackContext.success(startingValue);
    } else if (ACTION_STOP.equals(action)) {
        if (pActive) {
            Log.i(TAG, "Stopping StepCounterService");
            this.setPedometerIsActive(sharedPref, false);
            activity.stopService(stepCounterIntent);
        } else {
            Log.i(TAG, "StepCounterService already stopped");
        }

        if (bound) {
            Log.i(TAG, "Unbinding StepCounterService");
            activity.unbindService(mConnection);
        } else {
            Log.i(TAG, "StepCounterService already unbinded");
        }

        activity.stopService(stepCounterIntent);
        Integer currentCount = CordovaStepCounter.getTotalCount(sharedPref);
        //Callback with final count
        callbackContext.success(currentCount);
    } else if (ACTION_GET_STEPS.equals(action)) {
        if (!pActive) {
            Log.i(TAG, "Be Careful you're getting a Step count with inactive Pedometer");
        }

        Integer steps = CordovaStepCounter.getTotalCount(sharedPref);
        Log.i(TAG, "Geting steps counted from stepCounterService: " + steps);
        callbackContext.success(steps);
    } else if (ACTION_GET_TODAY_STEPS.equals(action)) {
        if (sharedPref.contains(PEDOMETER_HISTORY_PREF)) {
            String pDataString = sharedPref.getString(PEDOMETER_HISTORY_PREF, "{}");

            Date currentDate = new Date();
            SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
            String currentDateString = dateFormatter.format(currentDate);

            JSONObject pData = new JSONObject();
            JSONObject dayData = new JSONObject();
            Integer daySteps = -1;
            try {
                pData = new JSONObject(pDataString);
                Log.d(TAG, " got json shared prefs " + pData.toString());
            } catch (JSONException err) {
                Log.d(TAG, " Exception while parsing json string : " + pDataString);
            }

            if (pData.has(currentDateString)) {
                try {
                    dayData = pData.getJSONObject(currentDateString);
                    daySteps = dayData.getInt("steps");
                } catch (JSONException err) {
                    Log.e(TAG, "Exception while getting Object from JSON for " + currentDateString);
                }
            }

            Log.i(TAG, "Getting steps for today: " + daySteps);
            callbackContext.success(daySteps);
        } else {
            Log.i(TAG, "No steps history found in stepCounterService !");
            callbackContext.success(-1);
        }
    } else if (ACTION_GET_HISTORY.equals(action)) {
        if (sharedPref.contains(PEDOMETER_HISTORY_PREF)) {
            String pDataString = sharedPref.getString(PEDOMETER_HISTORY_PREF, "{}");
            Log.i(TAG, "Getting steps history from stepCounterService: " + pDataString);
            callbackContext.success(pDataString);
        } else {
            Log.i(TAG, "No steps history found in stepCounterService !");
            callbackContext.success("{}");
        }
    } else {
        Log.e(TAG, "Invalid action called on class " + TAG + ", " + action);
        callbackContext.error("Invalid action called on class " + TAG + ", " + action);
    }

    return result;
}