Advertisement
Guest User

Untitled

a guest
Jun 12th, 2015
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.19 KB | None | 0 0
  1. package com.tiesna.lokasi;
  2.  
  3. import android.app.Activity;
  4. import android.content.Intent;
  5. import android.location.Location;
  6. import android.os.Bundle;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.TextView;
  11. import android.widget.Toast;
  12.  
  13.  
  14.  
  15. import com.google.android.gms.common.ConnectionResult;
  16. import com.google.android.gms.common.GooglePlayServicesUtil;
  17. import com.google.android.gms.common.api.GoogleApiClient;
  18. import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
  19. import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
  20. import com.google.android.gms.location.LocationListener;
  21. import com.google.android.gms.location.LocationRequest;
  22. import com.google.android.gms.location.LocationServices;
  23.  
  24. public class LokasiActivity extends Activity implements ConnectionCallbacks,
  25. OnConnectionFailedListener, LocationListener {
  26.  
  27. // LogCat tag
  28. private static final String TAG = LokasiActivity.class.getSimpleName();
  29.  
  30. private final static int PLAY_SERVICES_RESOLUTION_REQUEST = 1000;
  31.  
  32. private Location mLastLocation;
  33.  
  34. // Google client to interact with Google API
  35. private GoogleApiClient mGoogleApiClient;
  36.  
  37. // boolean flag to toggle periodic location updates
  38. private boolean mRequestingLocationUpdates = false;
  39.  
  40. private LocationRequest mLocationRequest;
  41.  
  42. // Location updates intervals in sec
  43. private static int UPDATE_INTERVAL = 10000; // 10 sec
  44. private static int FATEST_INTERVAL = 5000; // 5 sec
  45. private static int DISPLACEMENT = 10; // 10 meters
  46.  
  47. // UI elements
  48. private TextView lblLocation;
  49. private Button btnShowLocation, btnStartLocationUpdates;
  50.  
  51. @Override
  52. protected void onCreate(Bundle savedInstanceState) {
  53. super.onCreate(savedInstanceState);
  54. setContentView(R.layout.activity_lokasi);
  55.  
  56. lblLocation = (TextView) findViewById(R.id.lblLocation);
  57. btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
  58. btnStartLocationUpdates = (Button) findViewById(R.id.btnLocationUpdates);
  59.  
  60. // First we need to check availability of play services
  61. if (checkPlayServices()) {
  62.  
  63. // Building the GoogleApi client
  64. buildGoogleApiClient();
  65.  
  66. createLocationRequest();
  67. }
  68.  
  69. // Show location button click listener
  70. btnShowLocation.setOnClickListener(new View.OnClickListener() {
  71.  
  72. @Override
  73. public void onClick(View v) {
  74. startActivity(new Intent(LokasiActivity.this, MapsActivity.class));
  75. finish();
  76. displayLocation();
  77. }
  78. });
  79.  
  80. // Toggling the periodic location updates
  81. btnStartLocationUpdates.setOnClickListener(new View.OnClickListener() {
  82.  
  83. @Override
  84. public void onClick(View v) {
  85. togglePeriodicLocationUpdates();
  86. }
  87. });
  88.  
  89. }
  90.  
  91. @Override
  92. protected void onStart() {
  93. super.onStart();
  94. if (mGoogleApiClient != null) {
  95. mGoogleApiClient.connect();
  96. }
  97. }
  98.  
  99. @Override
  100. protected void onResume() {
  101. super.onResume();
  102.  
  103. checkPlayServices();
  104.  
  105. // Resuming the periodic location updates
  106. if (mGoogleApiClient.isConnected() && mRequestingLocationUpdates) {
  107. startLocationUpdates();
  108. }
  109. }
  110.  
  111. @Override
  112. protected void onStop() {
  113. super.onStop();
  114. if (mGoogleApiClient.isConnected()) {
  115. mGoogleApiClient.disconnect();
  116. }
  117. }
  118.  
  119. @Override
  120. protected void onPause() {
  121. super.onPause();
  122. stopLocationUpdates();
  123. }
  124.  
  125. /**
  126. * Method to display the location on UI
  127. * */
  128. public void displayLocation() {
  129.  
  130. mLastLocation = LocationServices.FusedLocationApi
  131. .getLastLocation(mGoogleApiClient);
  132.  
  133. if (mLastLocation != null) {
  134. double latitude = mLastLocation.getLatitude();
  135. double longitude = mLastLocation.getLongitude();
  136.  
  137. lblLocation.setText(latitude + ", " + longitude);
  138.  
  139. } else {
  140. lblLocation.setText("(Couldn't get the location. Make sure location is enabled on the device)");
  141.  
  142. }
  143. }
  144.  
  145. /**
  146. * Method to toggle periodic location updates
  147. * */
  148. private void togglePeriodicLocationUpdates() {
  149. if (!mRequestingLocationUpdates) {
  150. // Changing the button text
  151. btnStartLocationUpdates
  152. .setText(getString(R.string.btn_stop_location_updates));
  153.  
  154. mRequestingLocationUpdates = true;
  155.  
  156. // Starting the location updates
  157. startLocationUpdates();
  158.  
  159. Log.d(TAG, "Periodic location updates started!");
  160.  
  161. } else {
  162. // Changing the button text
  163. btnStartLocationUpdates
  164. .setText(getString(R.string.btn_start_location_updates));
  165.  
  166. mRequestingLocationUpdates = false;
  167.  
  168. // Stopping the location updates
  169. stopLocationUpdates();
  170.  
  171. Log.d(TAG, "Periodic location updates stopped!");
  172. }
  173. }
  174.  
  175. /**
  176. * Creating google api client object
  177. * */
  178. protected synchronized void buildGoogleApiClient() {
  179. mGoogleApiClient = new GoogleApiClient.Builder(this)
  180. .addConnectionCallbacks(this)
  181. .addOnConnectionFailedListener(this)
  182. .addApi(LocationServices.API).build();
  183. }
  184.  
  185. /**
  186. * Creating location request object
  187. * */
  188. protected void createLocationRequest() {
  189. mLocationRequest = new LocationRequest();
  190. mLocationRequest.setInterval(UPDATE_INTERVAL);
  191. mLocationRequest.setFastestInterval(FATEST_INTERVAL);
  192. mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
  193. mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
  194. }
  195.  
  196. /**
  197. * Method to verify google play services on the device
  198. * */
  199. private boolean checkPlayServices() {
  200. int resultCode = GooglePlayServicesUtil
  201. .isGooglePlayServicesAvailable(this);
  202. if (resultCode != ConnectionResult.SUCCESS) {
  203. if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
  204. GooglePlayServicesUtil.getErrorDialog(resultCode, this,
  205. PLAY_SERVICES_RESOLUTION_REQUEST).show();
  206. } else {
  207. Toast.makeText(getApplicationContext(),
  208. "This device is not supported.", Toast.LENGTH_LONG)
  209. .show();
  210. finish();
  211. }
  212. return false;
  213. }
  214. return true;
  215. }
  216.  
  217. /**
  218. * Starting the location updates
  219. * */
  220. protected void startLocationUpdates() {
  221.  
  222. LocationServices.FusedLocationApi.requestLocationUpdates(
  223. mGoogleApiClient, mLocationRequest, this);
  224.  
  225. }
  226.  
  227. /**
  228. * Stop lokasi updates
  229. */
  230. protected void stopLocationUpdates() {
  231. LocationServices.FusedLocationApi.removeLocationUpdates(
  232. mGoogleApiClient, this);
  233. }
  234.  
  235. /**
  236. * Google api callback methods
  237. */
  238. @Override
  239. public void onConnectionFailed(ConnectionResult result) {
  240. Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
  241. + result.getErrorCode());
  242. }
  243.  
  244. @Override
  245. public void onConnected(Bundle arg0) {
  246.  
  247. // Once connected with google api, get the location
  248. displayLocation();
  249.  
  250. if (mRequestingLocationUpdates) {
  251. startLocationUpdates();
  252. }
  253. }
  254.  
  255. @Override
  256. public void onConnectionSuspended(int arg0) {
  257. mGoogleApiClient.connect();
  258. }
  259.  
  260. @Override
  261. public void onLocationChanged(Location location) {
  262. // Assign the new location
  263. mLastLocation = location;
  264.  
  265. Toast.makeText(getApplicationContext(), "Location changed!",
  266. Toast.LENGTH_SHORT).show();
  267.  
  268. // Displaying the new location on UI
  269. displayLocation();
  270. }
  271.  
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement