MCPcopy Create free account
hub / github.com/ZTFtrue/My-note / LocationUtils

Class LocationUtils

android/AndroidGPS.java:18–89  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16
17
18public class LocationUtils {
19 static LocationManager locationManager;
20 static Location location;
21 static boolean continueListener = true;
22
23 public Location init(Activity activity, Handler handler) {
24 String bestP = LocationManager.GPS_PROVIDER;
25 if (locationManager == null) {
26 locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
27
28 if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION)
29 != PackageManager.PERMISSION_GRANTED &&
30 ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION)
31 != PackageManager.PERMISSION_GRANTED) {
32 return null;
33 }
34
35 locationManager.addGpsStatusListener(new GpsStatus.Listener() {
36 @Override
37 public void onGpsStatusChanged(int event) {
38
39 }
40 });
41 bestP = locationManager.getBestProvider(getCri(), true);
42 String finalBestP = bestP;
43 handler.postDelayed(new Runnable() {
44 @SuppressLint("MissingPermission")
45 @Override
46 public void run() {
47 locationManager.requestLocationUpdates(finalBestP, 1000, 1, locationListener);
48 if (continueListener) {
49 handler.postDelayed(this, 1000 * 20);
50 }
51 }
52 }, 1000 * 20);
53 locationManager.requestLocationUpdates(bestP, 1000, 100, locationListener);
54 }
55 location = locationManager.getLastKnownLocation(bestP);
56 return location;
57 }
58
59 @SuppressLint("MissingPermission")
60 public static Location getLocation() {
61 if (location == null) {
62 String bestP = locationManager.getBestProvider(getCri(), true);
63 location = locationManager.getLastKnownLocation(bestP);
64 }
65 return location;
66 }
67
68 public static void setLocation(Location location) {
69 LocationUtils.location = location;
70 }
71
72 LocationListener locationListener = new LocationListener() {
73 @Override
74 public void onLocationChanged(@NonNull Location location) {
75 LocationUtils.location = location;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected