daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Jan 29th, 2018 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package fr.acarmona.com.tpandroid;
  2.  
  3. import android.Manifest;
  4. import android.annotation.SuppressLint;
  5. import android.content.Intent;
  6. import android.content.pm.PackageManager;
  7. import android.location.Location;
  8. import android.os.Bundle;
  9. import android.os.Handler;
  10. import android.os.SystemClock;
  11. import android.support.annotation.NonNull;
  12. import android.support.annotation.Nullable;
  13. import android.support.v4.app.ActivityCompat;
  14. import android.support.wearable.activity.WearableActivity;
  15. import android.widget.FrameLayout;
  16. import android.widget.TextView;
  17. import android.widget.Toast;
  18.  
  19. import com.google.android.gms.common.ConnectionResult;
  20. import com.google.android.gms.common.api.GoogleApiClient;
  21. import com.google.android.gms.location.FusedLocationProviderClient;
  22. import com.google.android.gms.location.LocationCallback;
  23. import com.google.android.gms.location.LocationListener;
  24. import com.google.android.gms.location.LocationRequest;
  25. import com.google.android.gms.location.LocationResult;
  26. import com.google.android.gms.location.LocationServices;
  27. import com.google.android.gms.tasks.OnSuccessListener;
  28.  
  29. import java.util.concurrent.TimeUnit;
  30.  
  31. /**
  32.  * Created by Proprietaire on 23/01/2018.
  33.  */
  34.  
  35. public class SportActivity extends WearableActivity {
  36.  
  37.     TextView distance;
  38.     TextView timer;
  39.     FrameLayout frame;
  40.  
  41.     private long startTime = 0L;
  42.     long timeInMilliseconds = 0L;
  43.     long timeSwapBuff = 0L;
  44.     long updatedTime = 0L;
  45.  
  46.     private Handler handler = new Handler();
  47.  
  48.     private FusedLocationProviderClient mFusedLocationClient;
  49.  
  50.     @SuppressLint("MissingPermission")
  51.     protected void onCreate(Bundle savedInstanceState) {
  52.         super.onCreate(savedInstanceState);
  53.         setContentView(R.layout.activity_sport);
  54.  
  55.         frame = (FrameLayout) findViewById(R.id.frame);
  56.  
  57.         //Get intent for color
  58.         Intent intent = getIntent();
  59.         if (intent.hasExtra("color")) {
  60.             frame.setBackgroundColor(intent.getIntExtra("color",0));
  61.         }
  62.  
  63.         distance = (TextView) findViewById(R.id.distance_parcouru);
  64.         timer = (TextView) findViewById(R.id.timer);
  65.         //Handler timer
  66.         startTime = SystemClock.uptimeMillis();
  67.         handler.postDelayed(updateTimerThread, 0);
  68.  
  69.         mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
  70.  
  71.         mFusedLocationClient.getLastLocation()
  72.                 .addOnSuccessListener(this, new OnSuccessListener<Location>() {
  73.                     @Override
  74.                     public void onSuccess(Location location) {
  75.                         // Got last known location. In some rare situations this can be null.
  76.                         if (location != null) {
  77.                             // Logic to handle location object
  78.                             Toast.makeText(getApplicationContext(), "TEST", Toast.LENGTH_SHORT);
  79.                         }
  80.                     }
  81.                 });
  82.  
  83.         startLocationUpdates();
  84.         setAmbientEnabled();
  85.     }
  86.  
  87.     @SuppressLint("MissingPermission")
  88.     private void startLocationUpdates() {
  89.  
  90.         LocationRequest mLocationRequest = LocationRequest.create();
  91.         // Use high accuracy
  92.         mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
  93.         // Set the update interval to 2 seconds
  94.         mLocationRequest.setInterval(TimeUnit.SECONDS.toMillis(2));
  95.         // Set the fastest update interval to 2 seconds
  96.         mLocationRequest.setFastestInterval(TimeUnit.SECONDS.toMillis(2));
  97.         // Set the minimum displacement
  98.         mLocationRequest.setSmallestDisplacement(2);
  99.         LocationCallback mLocationCallback;
  100.  
  101.         mLocationCallback = new LocationCallback() {
  102.             @Override
  103.             public void onLocationResult(LocationResult locationResult) {
  104.                 for (Location location : locationResult.getLocations()) {
  105.                     // Update UI with location data
  106.                     // ...
  107.                     distance.setText("Latitude:  " + String.valueOf( location.getLatitude()) +
  108.                             "\nLongitude:  " + String.valueOf( location.getLongitude()));
  109.                 }
  110.             };
  111.         };
  112.  
  113.         mFusedLocationClient.requestLocationUpdates(mLocationRequest,
  114.                 mLocationCallback,
  115.                 null /* Looper */);
  116.     }
  117.  
  118.     private Runnable updateTimerThread = new Runnable() {
  119.  
  120.         @Override
  121.         public void run() {
  122.  
  123.             timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
  124.  
  125.             updatedTime = timeSwapBuff + timeInMilliseconds;
  126.  
  127.             int secs = (int) (updatedTime / 1000);
  128.             int mins = secs / 60;
  129.             secs = secs % 60;
  130.             int milliseconds = (int) (updatedTime % 1000);
  131.  
  132.             timer.setText(" " + mins + ":"
  133.                             + String.format("%02d", secs) + ":"
  134.                             + String.format("%03d", milliseconds));
  135.             handler.postDelayed(this, 0);
  136.  
  137.         }
  138.     };
  139.  
  140.     @SuppressLint("MissingPermission")
  141.     public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
  142.         Toast.makeText(getApplicationContext(),"Deni location",Toast.LENGTH_SHORT).show();
  143.         switch (requestCode) {
  144.             case 2: {
  145.                 // If request is cancelled, the result arrays are empty.
  146.                 if (grantResults.length > 0
  147.                         && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  148.  
  149.                 } else {
  150.  
  151.                     // permission denied, boo! Disable the
  152.                     // functionality that depends on this permission.
  153.                 }
  154.                 return;
  155.             }
  156.  
  157.             // other 'case' lines to check for other
  158.             // permissions this app might request.
  159.         }
  160.  
  161.     }
  162. }
RAW Paste Data
Pastebin PRO WINTER Special!
Get 40% OFF Pastebin PRO accounts!
Top