Created
March 5, 2019 04:36
-
-
Save fauzisho/8d83ede4d114a31c33caf12e484de32c to your computer and use it in GitHub Desktop.
location services
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -> Android manifest | |
| <service | |
| android:name=".service.LocationService" | |
| android:enabled="true" | |
| android:exported="true" /> | |
| ----------------------------------------------------------------------------------------------------------------- | |
| -> public class locationSevices | |
| public class LocationService extends Service implements LocationListener { | |
| private String requesterId; | |
| private LocationManager lm; | |
| public LocationService() { | |
| } | |
| @Override | |
| public IBinder onBind(Intent intent) { | |
| // TODO: Return the communication channel to the service. | |
| throw new UnsupportedOperationException("Not yet implemented"); | |
| } | |
| @Override | |
| public int onStartCommand(Intent intent, int flags, int startId) { | |
| if (intent != null) { | |
| requesterId = intent.getStringExtra("EXTRA_REQUESTER_ID"); | |
| addLocationListener(); | |
| } | |
| return START_STICKY; | |
| } | |
| @Override | |
| public void onDestroy() { | |
| ToastUtil.show(this, "berhenti membagikan"); | |
| if (lm != null) { | |
| lm.removeUpdates(this); | |
| lm = null; | |
| } | |
| super.onDestroy(); | |
| } | |
| @SuppressLint("CheckResult") | |
| private void addLocationListener() { | |
| lm = (LocationManager) getSystemService(LOCATION_SERVICE); | |
| assert lm != null; | |
| if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) | |
| != PackageManager.PERMISSION_GRANTED | |
| && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) | |
| != PackageManager.PERMISSION_GRANTED) { | |
| return; | |
| } | |
| Location lastKnown = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER) != null | |
| ? lm.getLastKnownLocation(LocationManager.GPS_PROVIDER) : lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); | |
| if (lastKnown != null) { | |
| postUpdateLocation(lastKnown.getLatitude(), lastKnown.getLongitude()); | |
| } | |
| lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 20, this); | |
| } | |
| @SuppressLint("CheckResult") | |
| private void postUpdateLocation(double lat, double lng) { | |
| //post update location in here | |
| } | |
| @Override | |
| public void onLocationChanged(Location location) { | |
| postUpdateLocation(location.getLatitude(), location.getLongitude()); | |
| } | |
| @Override | |
| public void onStatusChanged(String provider, int status, Bundle extras) { | |
| } | |
| @Override | |
| public void onProviderEnabled(String provider) { | |
| } | |
| @Override | |
| public void onProviderDisabled(String provider) { | |
| } | |
| } | |
| ------------------------------------------------------------------------------------------------ | |
| How to activate : | |
| private Intent locationService = new Intent(App.getContext(), LocationService.class); | |
| private void startLocationService() { | |
| locationService.putExtra("EXTRA_REQUESTER_ID", requesterId); | |
| startService(locationService); | |
| } | |
| private void stopLocationService() { | |
| stopService(locationService); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment