SHARE
TWEET
Untitled
a guest
Jan 29th, 2018
47
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- package fr.acarmona.com.tpandroid;
- import android.Manifest;
- import android.annotation.SuppressLint;
- import android.content.Intent;
- import android.content.pm.PackageManager;
- import android.location.Location;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.SystemClock;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.v4.app.ActivityCompat;
- import android.support.wearable.activity.WearableActivity;
- import android.widget.FrameLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.api.GoogleApiClient;
- import com.google.android.gms.location.FusedLocationProviderClient;
- import com.google.android.gms.location.LocationCallback;
- import com.google.android.gms.location.LocationListener;
- import com.google.android.gms.location.LocationRequest;
- import com.google.android.gms.location.LocationResult;
- import com.google.android.gms.location.LocationServices;
- import com.google.android.gms.tasks.OnSuccessListener;
- import java.util.concurrent.TimeUnit;
- /**
- * Created by Proprietaire on 23/01/2018.
- */
- public class SportActivity extends WearableActivity {
- TextView distance;
- TextView timer;
- FrameLayout frame;
- private long startTime = 0L;
- long timeInMilliseconds = 0L;
- long timeSwapBuff = 0L;
- long updatedTime = 0L;
- private Handler handler = new Handler();
- private FusedLocationProviderClient mFusedLocationClient;
- @SuppressLint("MissingPermission")
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_sport);
- frame = (FrameLayout) findViewById(R.id.frame);
- //Get intent for color
- Intent intent = getIntent();
- if (intent.hasExtra("color")) {
- frame.setBackgroundColor(intent.getIntExtra("color",0));
- }
- distance = (TextView) findViewById(R.id.distance_parcouru);
- timer = (TextView) findViewById(R.id.timer);
- //Handler timer
- startTime = SystemClock.uptimeMillis();
- handler.postDelayed(updateTimerThread, 0);
- mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
- mFusedLocationClient.getLastLocation()
- .addOnSuccessListener(this, new OnSuccessListener<Location>() {
- @Override
- public void onSuccess(Location location) {
- // Got last known location. In some rare situations this can be null.
- if (location != null) {
- // Logic to handle location object
- Toast.makeText(getApplicationContext(), "TEST", Toast.LENGTH_SHORT);
- }
- }
- });
- startLocationUpdates();
- setAmbientEnabled();
- }
- @SuppressLint("MissingPermission")
- private void startLocationUpdates() {
- LocationRequest mLocationRequest = LocationRequest.create();
- // Use high accuracy
- mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
- // Set the update interval to 2 seconds
- mLocationRequest.setInterval(TimeUnit.SECONDS.toMillis(2));
- // Set the fastest update interval to 2 seconds
- mLocationRequest.setFastestInterval(TimeUnit.SECONDS.toMillis(2));
- // Set the minimum displacement
- mLocationRequest.setSmallestDisplacement(2);
- LocationCallback mLocationCallback;
- mLocationCallback = new LocationCallback() {
- @Override
- public void onLocationResult(LocationResult locationResult) {
- for (Location location : locationResult.getLocations()) {
- // Update UI with location data
- // ...
- distance.setText("Latitude: " + String.valueOf( location.getLatitude()) +
- "\nLongitude: " + String.valueOf( location.getLongitude()));
- }
- };
- };
- mFusedLocationClient.requestLocationUpdates(mLocationRequest,
- mLocationCallback,
- null /* Looper */);
- }
- private Runnable updateTimerThread = new Runnable() {
- @Override
- public void run() {
- timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
- updatedTime = timeSwapBuff + timeInMilliseconds;
- int secs = (int) (updatedTime / 1000);
- int mins = secs / 60;
- secs = secs % 60;
- int milliseconds = (int) (updatedTime % 1000);
- timer.setText(" " + mins + ":"
- + String.format("%02d", secs) + ":"
- + String.format("%03d", milliseconds));
- handler.postDelayed(this, 0);
- }
- };
- @SuppressLint("MissingPermission")
- public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
- Toast.makeText(getApplicationContext(),"Deni location",Toast.LENGTH_SHORT).show();
- switch (requestCode) {
- case 2: {
- // If request is cancelled, the result arrays are empty.
- if (grantResults.length > 0
- && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- } else {
- // permission denied, boo! Disable the
- // functionality that depends on this permission.
- }
- return;
- }
- // other 'case' lines to check for other
- // permissions this app might request.
- }
- }
- }
RAW Paste Data

