List of usage examples for android.app Activity stopService
@Override public boolean stopService(Intent name)
From source file:com.marianhello.cordova.bgloc.BackgroundGpsPlugin.java
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) { Activity activity = this.cordova.getActivity(); Context context = activity.getApplicationContext(); Boolean result = false;//from w ww . java 2 s . c om updateServiceIntent = new Intent(activity, LocationUpdateService.class); if (ACTION_START.equalsIgnoreCase(action) && !isEnabled) { result = true; if (params == null || headers == null) { callbackContext.error("Call configure before calling start"); } else { IntentFilter intentFilter = new IntentFilter(Constant.FILTER); // LocalBroadcastManager.getInstance(activity).registerReceiver(mMessageReceiver, intentFilter); context.registerReceiver(mMessageReceiver, intentFilter); updateServiceIntent.putExtra("url", url); updateServiceIntent.putExtra("params", params); updateServiceIntent.putExtra("headers", headers); updateServiceIntent.putExtra("stationaryRadius", stationaryRadius); updateServiceIntent.putExtra("desiredAccuracy", desiredAccuracy); updateServiceIntent.putExtra("distanceFilter", distanceFilter); updateServiceIntent.putExtra("locationTimeout", locationTimeout); updateServiceIntent.putExtra("desiredAccuracy", desiredAccuracy); updateServiceIntent.putExtra("isDebugging", isDebugging); updateServiceIntent.putExtra("notificationTitle", notificationTitle); updateServiceIntent.putExtra("notificationText", notificationText); updateServiceIntent.putExtra("stopOnTerminate", stopOnTerminate); activity.startService(updateServiceIntent); isEnabled = true; Log.d(TAG, "bg service has been started"); } } else if (ACTION_STOP.equalsIgnoreCase(action)) { // LocalBroadcastManager.getInstance(activity).unregisterReceiver(mMessageReceiver); context.unregisterReceiver(mMessageReceiver); isEnabled = false; result = true; activity.stopService(updateServiceIntent); callbackContext.success(); Log.d(TAG, "bg service has been stopped"); } else if (ACTION_CONFIGURE.equalsIgnoreCase(action)) { result = true; try { this.callbackContext = callbackContext; // Params. // 0 1 2 3 4 5 6 7 8 9 10 11 //[params, headers, url, stationaryRadius, distanceFilter, locationTimeout, desiredAccuracy, debug, notificationTitle, notificationText, activityType, stopOnTerminate] this.params = data.getString(0); this.headers = data.getString(1); this.url = data.getString(2); this.stationaryRadius = data.getString(3); this.distanceFilter = data.getString(4); this.locationTimeout = data.getString(5); this.desiredAccuracy = data.getString(6); this.isDebugging = data.getString(7); this.notificationTitle = data.getString(8); this.notificationText = data.getString(9); this.stopOnTerminate = data.getString(11); Log.d(TAG, "bg service configured"); } catch (JSONException e) { callbackContext.error("authToken/url required as parameters: " + e.getMessage()); } } else if (ACTION_SET_CONFIG.equalsIgnoreCase(action)) { result = true; // TODO reconfigure Service callbackContext.success(); Log.d(TAG, "bg service reconfigured"); } else if (ACTION_LOCATION_ENABLED_CHECK.equalsIgnoreCase(action)) { Log.d(TAG, "location services enabled check"); try { int isLocationEnabled = BackgroundGpsPlugin.isLocationEnabled(context) ? 1 : 0; callbackContext.success(isLocationEnabled); } catch (SettingNotFoundException e) { callbackContext.error("Location setting not found on this platform"); } } return result; }
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;// w w w . j ava 2 s . c om 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; }