Code example for ScheduledThreadPoolExecutor

Methods: execute, shutdown

0
		public LocationServiceThread(String url, String email, String password) {
			this.url = url;
			this.email = email;
			this.password = password;
			active = true;
			scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1);
			setStatus(ACTIVE);
		} 
 
		public void stop() { 
			scheduledThreadPoolExecutor.shutdown();
		} 
 
		public void onLocationChanged(Location location) {
			// Log.i(TAG, "[" + location.getProvider() + "](" + location.getLatitude() + "," + location.getLongitude() + ") accuracy: " + location.getAccuracy()); 
			if (!active) {
				//Log.i(TAG, "But the LocationService is inactive, so not storing it."); 
				return; 
			} 
			lastLocation = location;
			lastLocations.put(location.getProvider(), location);
		} 
 
		private void ping() { 
			String pingStatus = this.status;
 
			String localDate;
			 
			localDate = dateFormat.format(new Date());
 
			HttpClient client = HttpUtils.getNewHttpClient();
			HttpPost request = new HttpPost(url);
			try { 
				List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);
				nameValuePairs.add(new BasicNameValuePair(
						"user[email]", email));
				nameValuePairs.add(new BasicNameValuePair(
						"user[password]", password));
 
				Location location = lastLocation;
				if (location != null) {
					nameValuePairs.add(new BasicNameValuePair(
							"device_pool_driver[lat]", 
							Double.toString(location.getLatitude())));
					nameValuePairs.add(new BasicNameValuePair(
							"device_pool_driver[lng]", 
							Double.toString(location.getLongitude())));
				} else 
					Log.i(TAG, "Missing location on ping");
				 
				// append all known locations at this time, for server reporting 
				Iterator<String> iter = lastLocations.keySet().iterator();
				while (iter.hasNext()) {
					String provider = iter.next();
					Location oneLocation = lastLocations.get(provider);
					String prefix = "location[" + provider + "]";
					 
					 
					nameValuePairs.add(new BasicNameValuePair(
							prefix + "[lat]",
							Double.toString(location.getLatitude())));
					 
					nameValuePairs.add(new BasicNameValuePair(
							prefix + "[lng]",
							Double.toString(location.getLongitude())));
					 
					nameValuePairs.add(new BasicNameValuePair(
							prefix + "[accuracy]",
							Double.toString(location.getAccuracy())));
					 
					nameValuePairs.add(new BasicNameValuePair(
							prefix + "[time]",
							dateFormat.format(new Date(oneLocation.getTime()))));
				} 
				 
				nameValuePairs.add(new BasicNameValuePair(
						"device_pool_driver[status]", 
						pingStatus));
 
				nameValuePairs.add(new BasicNameValuePair(
						"device_pool_driver[posted_at]", localDate));
 
				request.setEntity(new UrlEncodedFormEntity(
						nameValuePairs));
				 
				// beware, uncommenting this will show the password in the device logs!  
				// Log.i(TAG, "Posting to URL " + url + " with " + HttpUtils.pairsToString(nameValuePairs)); 
 
				HttpResponse response = client.execute(request);
 
				if (response.getStatusLine().getStatusCode() == 200) {
					HttpEntity entity = response.getEntity();
					String json = EntityUtils.toString(entity);
					JSONTokener tokener = new JSONTokener(json);
					JSONObject data = (JSONObject) tokener.nextValue();
					if (data.has("device_pool_driver")) {
						activity.ping();
						return; //success! 
					} 
					Log.e(TAG, "data was " + data);
					Log.e(TAG, "json was " + json);
				} 
			} catch (ClientProtocolException e) {
				Log.e(TAG, "protocol exception sending ping", e);
			} catch (IOException e) {
				Log.e(TAG, "IO exception sending ping", e);
			} catch (JSONException e) {
				Log.e(TAG, "bad json from server while pinging", e);
			} catch (Exception e) {
				Log.e(TAG, "some other problem sending ping", e);
			} 
			 
			return; 
		} 
 
		public void onProviderDisabled(String provider) {
			//Log.i(TAG, "provider disabled " + provider); 
		} 
 
		public void onProviderEnabled(String provider) {
			//Log.i(TAG, "provider enabled " + provider); 
		} 
 
		public void onStatusChanged(String provider, int status, Bundle extras) {
			//Log.i(TAG, "on status changed " + provider + " status = " + status); 
		} 
 
		public void toast(String message) {
			if (!active) {
				return; 
			} 
			if (activity != null) {
				activity.toast(message);
			} 
		} 
 
		public void run() { 
			scheduledThreadPoolExecutor.scheduleAtFixedRate(pingTask, 0, pingInterval, TimeUnit.SECONDS);
			 
			Looper.prepare();
			 
			locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
			 
			// initialize from the activity, on the UI thread 
			activity.startLocation();
		} 
 
		public void setStatus(String status) {
			this.status = status;
			scheduledThreadPoolExecutor.execute(forcePingTask);
			if (status.equals(INACTIVE)) {
				Log.i(TAG, "Shutting down thread, service marked as inactive");
				scheduledThreadPoolExecutor.shutdown();
			} 
		} 
 
		public String getStatus() {
			return status;