Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.facticoapp.supercivicos.services;
- import android.app.Activity;
- import android.app.Service;
- import android.content.Context;
- import android.content.Intent;
- import android.location.Location;
- import android.os.IBinder;
- import android.support.v4.content.LocalBroadcastManager;
- import android.util.Log;
- import com.facticoapp.supercivicos.dialogues.Dialogues;
- import com.facticoapp.supercivicos.location.CustomLocation;
- import com.facticoapp.supercivicos.location.LocationClientListener;
- import com.facticoapp.supercivicos.preferences.PreferencesManager;
- /**
- * Created by Edgar Z. on 27/01/16.
- */
- public class LocationService extends Service {
- public final String TAG_CLASS = LocationService.class.getName();
- private LocationClientListener clientListener;
- public static final String LOCATION = "location";
- public static final String LOCATION_LAT = "lat";
- public static final String LOCATION_LON = "lon";
- @Override
- public void onCreate() {
- super.onCreate();
- Dialogues.Log(TAG_CLASS, "onCreate", Log.DEBUG);
- clientListener = new LocationClientListener(getBaseContext());
- clientListener.setOnLocationClientListener(onLocationClientListener);
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- Dialogues.Log(TAG_CLASS, "onStartCommand", Log.DEBUG);
- if (clientListener != null) clientListener.connect();
- //return super.onStartCommand(intent, flags, startId);
- return Service.START_STICKY;
- }
- private LocationClientListener.OnLocationClientListener onLocationClientListener = new LocationClientListener.OnLocationClientListener() {
- @Override
- public void onLocationChanged(Location location) {
- Dialogues.Log(TAG_CLASS, "Service Latitude: " + location.getLatitude() + ", Longitude" + location.getLongitude(), Log.ERROR);
- PreferencesManager.putLocationPreference(getApplication(),
- String.valueOf(location.getLatitude()),
- String.valueOf(location.getLongitude()));
- sendLocationBroadcast(location);
- stopSelf();
- }
- };
- @Override
- public void onDestroy() {
- Dialogues.Log(TAG_CLASS, "onDestroy", Log.DEBUG);
- clientListener.disconnect();
- super.onDestroy();
- }
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- private void sendLocationBroadcast(Location location) {
- Intent intent = new Intent(LocationService.LOCATION);
- intent.putExtra(LOCATION_LAT, String.valueOf(location.getLatitude()));
- intent.putExtra(LOCATION_LON, String.valueOf(location.getLongitude()));
- LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement