Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. package com.example.hp.tumg13;
  2.  
  3. import android.location.Location;
  4. import android.os.Bundle;
  5. import android.support.v7.app.ActionBarActivity;
  6. import android.util.Log;
  7. import android.widget.TextView;
  8.  
  9. import com.google.android.gms.common.ConnectionResult;
  10. import com.google.android.gms.common.api.GoogleApiClient;
  11. import com.google.android.gms.location.LocationServices;
  12.  
  13.  
  14. public class MainActivity extends ActionBarActivity
  15. implements GoogleApiClient.ConnectionCallbacks,
  16. GoogleApiClient.OnConnectionFailedListener{
  17.  
  18. private TextView tvCoordinate;
  19. private GoogleApiClient mGoogleApiClient;
  20.  
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25.  
  26. tvCoordinate = (TextView) findViewById(R.id.Localizacaotv);
  27.  
  28. callConnection();
  29. }
  30.  
  31.  
  32. private synchronized void callConnection(){
  33. mGoogleApiClient = new GoogleApiClient.Builder(this)
  34. .addOnConnectionFailedListener(this)
  35. .addConnectionCallbacks(this)
  36. .addApi(LocationServices.API)
  37. .build();
  38. mGoogleApiClient.connect();
  39. }
  40.  
  41.  
  42. // LISTENER
  43. @Override
  44. public void onConnected(Bundle bundle) {
  45. Log.i("LOG", "onConnected(" + bundle + ")");
  46.  
  47. Location l = LocationServices
  48. .FusedLocationApi
  49. .getLastLocation(mGoogleApiClient);
  50.  
  51. if(l != null){
  52. Log.i("LOG", "latitude: "+l.getLatitude());
  53. Log.i("LOG", "longitude: "+l.getLongitude());
  54. tvCoordinate.setText(l.getLatitude()+" | "+l.getLongitude());
  55. }
  56. }
  57.  
  58. @Override
  59. public void onConnectionSuspended(int i) {
  60. Log.i("LOG", "onConnectionSuspended(" + i + ")");
  61. }
  62. @Override
  63. public void onConnectionFailed(ConnectionResult connectionResult) {
  64. Log.i("LOG", "onConnectionFailed("+connectionResult+")");
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement