Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package app.LocationService.Service;
- /**
- * Created by AhmadYusri on 11/11/2015.
- */
- import android.app.Service;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.location.Location;
- import android.os.Binder;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.os.Messenger;
- import android.support.v4.content.LocalBroadcastManager;
- import android.util.Log;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.GooglePlayServicesUtil;
- import com.google.android.gms.common.api.GoogleApiClient;
- import com.google.android.gms.location.LocationListener;
- import com.google.android.gms.location.LocationRequest;
- import com.google.android.gms.location.LocationServices;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import app.fikri.com.labaikallahsystem.Constants;
- public class LocationService extends Service implements
- GoogleApiClient.ConnectionCallbacks,
- GoogleApiClient.OnConnectionFailedListener,
- LocationListener {
- private static final String TAG = "LocationService";
- public final static String ACTION = "LOCATION_ACTION";
- public final static String LATITUDE = "LOCATION_LATITUDE";
- public final static String LONGITUDE = "LOCATION_LONGITUDE";
- // use the websmithing defaultUploadWebsite for testing and then check your
- // location with your browser here: https://www.websmithing.com/gpstracker/displaymap.php
- private String defaultUploadWebsite;
- private boolean currentlyProcessingLocation = false;
- private LocationRequest locationRequest;
- private GoogleApiClient locationClient;
- @Override
- public void onCreate() {
- super.onCreate();
- //defaultUploadWebsite = getString(R.string.default_upload_website);
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- // if we are currently trying to get a location and the alarm manager has called this again,
- // no need to start processing a new location.
- if (!currentlyProcessingLocation) {
- currentlyProcessingLocation = true;
- startTracking();
- }
- return START_NOT_STICKY;
- }
- private void startTracking() {
- Log.d(TAG, "startTracking");
- if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) == ConnectionResult.SUCCESS) {
- locationClient = new GoogleApiClient.Builder(this)
- .addApi(LocationServices.API)
- .addConnectionCallbacks(this)
- .addOnConnectionFailedListener(this).build();
- if (!locationClient.isConnected() || !locationClient.isConnecting()) {
- locationClient.connect();
- }
- } else {
- Log.e(TAG, "unable to connect to google play services.");
- }
- }
- protected void sendLocationDataToWebsite(Location location) {
- // formatted for mysql datetime format
- Log.i(TAG, String.valueOf(location.getLatitude()) + " " + String.valueOf(location.getLongitude()));
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- stopLocationUpdates();
- stopSelf();
- }
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onLocationChanged(Location location) {
- if (location != null) {
- Log.e(TAG, "position: " + location.getLatitude() + ", " + location.getLongitude() + " accuracy: " + location.getAccuracy());
- // we have our desired accuracy of 500 meters so lets quit this service,
- // onDestroy will be called and stop our location uodates
- broadcastLocation(location.getLatitude(), location.getLongitude());
- //sendLocationDataToWebsite(location);
- }
- }
- private void stopLocationUpdates() {
- if (locationClient != null && locationClient.isConnected()) {
- LocationServices.FusedLocationApi.removeLocationUpdates(locationClient, this);
- locationClient.disconnect();
- }
- }
- /**
- * Called by Location Services when the request to connect the
- * client finishes successfully. At this point, you can
- * request the current location or start periodic updates
- */
- @Override
- public void onConnected(Bundle bundle) {
- Log.d(TAG, "onConnected");
- locationRequest = new LocationRequest();
- locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
- locationRequest.setInterval(Constants.UPDATE_INTERVAL);
- locationRequest.setFastestInterval(Constants.FASTEST_INTERVAL);
- LocationServices.FusedLocationApi.requestLocationUpdates(locationClient,
- locationRequest, this);
- }
- @Override
- public void onConnectionSuspended(int i) {
- stopLocationUpdates();
- }
- /**
- * Called by Location Services if the connection to the
- * location client drops because of an error.
- */
- @Override
- public void onConnectionFailed(ConnectionResult connectionResult) {
- Log.e(TAG, "onConnectionFailed");
- stopLocationUpdates();
- stopSelf();
- }
- public String getCurrentTime()
- {
- SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
- String currentTime = sdf.format(new Date());
- return currentTime;
- }
- public String getDeviceName() {
- String manufacturer = Build.MANUFACTURER;
- String model = Build.MODEL;
- if (model.startsWith(manufacturer)) {
- return capitalize(model);
- } else {
- return capitalize(manufacturer) + " " + model;
- }
- }
- private String capitalize(String s) {
- if (s == null || s.length() == 0) {
- return "";
- }
- char first = s.charAt(0);
- if (Character.isUpperCase(first)) {
- return s;
- } else {
- return Character.toUpperCase(first) + s.substring(1);
- }
- }
- private void broadcastLocation(double lat, double lng)
- {
- Intent intent = new Intent();
- intent.setAction(ACTION);
- intent.putExtra(LATITUDE, lat);
- intent.putExtra(LONGITUDE, lng);
- sendBroadcast(intent);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement