Guest User

ListFragment.java

a guest
Jun 19th, 2022
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.85 KB | None | 0 0
  1. package com.maxet24.chargely;
  2.  
  3. import android.Manifest;
  4. import android.location.Location;
  5. import android.os.Bundle;
  6.  
  7. import androidx.core.app.ActivityCompat;
  8. import androidx.fragment.app.Fragment;
  9.  
  10. import android.view.LayoutInflater;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.widget.TextView;
  14.  
  15. import com.google.android.gms.location.FusedLocationProviderClient;
  16. import com.google.android.gms.location.LocationServices;
  17. import com.google.android.gms.tasks.OnSuccessListener;
  18.  
  19. public class ListFragment extends Fragment {
  20.  
  21.     private TextView longText;
  22.     private TextView latText;
  23.  
  24.     private FusedLocationProviderClient fusedLocationClient;
  25.  
  26.     @Override
  27.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  28.                              Bundle savedInstanceState) {
  29.         View v = inflater.inflate(R.layout.fragment_list, container, false);
  30.  
  31.         // Init TextView
  32.         longText = (TextView) v.findViewById(R.id.longTest);
  33.         latText = (TextView) v.findViewById(R.id.latTest);
  34.  
  35.         // Get Location according to guide
  36.         fusedLocationClient = LocationServices.getFusedLocationProviderClient(getContext());
  37.  
  38.         fusedLocationClient.getLastLocation()
  39.                 .addOnSuccessListener(this, new OnSuccessListener<Location>() {
  40.                     @Override
  41.                     public void onSuccess(Location location) {
  42.                         // Got last known location. In some rare situations this can be null.
  43.                         if (location != null) {
  44.                             longText.setText("Longitude: " + Double.toString(location.getLongitude()));
  45.                             latText.setText("latitude: " + Double.toString(location.getLatitude()));
  46.                         }
  47.                     }
  48.                 });
  49.  
  50.  
  51.         return v;
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment