Browse Source

-Add simple Location for backup if gms location not work

ilhamitubagoes 4 years ago
parent
commit
37bd32a90e
2 changed files with 29 additions and 0 deletions
  1. 3 0
      app/build.gradle
  2. 26 0
      app/src/main/java/com/fusi24/rfid/ui/ScanActivity.java

+ 3 - 0
app/build.gradle

@@ -81,6 +81,9 @@ dependencies {
81
     // Toggle Button
81
     // Toggle Button
82
     implementation 'com.github.angads25:toggle:1.1.0'
82
     implementation 'com.github.angads25:toggle:1.1.0'
83
 
83
 
84
+    // Simple Location
85
+    implementation 'com.github.delight-im:Android-SimpleLocation:v1.1.0'
86
+
84
     //Serial Port
87
     //Serial Port
85
     implementation files('libs/serialport.jar')
88
     implementation files('libs/serialport.jar')
86
     implementation files('libs/android-async-http-1.4.9.jar')
89
     implementation files('libs/android-async-http-1.4.9.jar')

+ 26 - 0
app/src/main/java/com/fusi24/rfid/ui/ScanActivity.java

@@ -27,6 +27,7 @@ import com.google.android.gms.location.LocationListener;
27
 import com.google.android.gms.location.LocationRequest;
27
 import com.google.android.gms.location.LocationRequest;
28
 import com.google.android.gms.location.LocationServices;
28
 import com.google.android.gms.location.LocationServices;
29
 
29
 
30
+import im.delight.android.location.SimpleLocation;
30
 import timber.log.Timber;
31
 import timber.log.Timber;
31
 
32
 
32
 public class ScanActivity extends LFScanProcessing implements GoogleApiClient.ConnectionCallbacks,
33
 public class ScanActivity extends LFScanProcessing implements GoogleApiClient.ConnectionCallbacks,
@@ -49,6 +50,9 @@ public class ScanActivity extends LFScanProcessing implements GoogleApiClient.Co
49
     private double currentLatitude;
50
     private double currentLatitude;
50
     private double currentLongitude;
51
     private double currentLongitude;
51
 
52
 
53
+    //Define a simple location android
54
+    private SimpleLocation simpleLocation;
55
+
52
     @Override
56
     @Override
53
     protected void onCreate(Bundle savedInstanceState) {
57
     protected void onCreate(Bundle savedInstanceState) {
54
         super.onCreate(savedInstanceState);
58
         super.onCreate(savedInstanceState);
@@ -82,6 +86,14 @@ public class ScanActivity extends LFScanProcessing implements GoogleApiClient.Co
82
                 .setInterval(10 * 1000)        // 10 seconds, in milliseconds
86
                 .setInterval(10 * 1000)        // 10 seconds, in milliseconds
83
                 .setFastestInterval(1000); // 1 second, in milliseconds
87
                 .setFastestInterval(1000); // 1 second, in milliseconds
84
 
88
 
89
+        // First Time init
90
+        simpleLocation = new SimpleLocation(this);
91
+        // if we can't access the location yet
92
+        if (!simpleLocation.hasLocationEnabled()) {
93
+            // ask the user to enable location access
94
+            SimpleLocation.openSettings(this);
95
+        }
96
+
85
         initView();
97
         initView();
86
         initSound();
98
         initSound();
87
     }
99
     }
@@ -97,6 +109,7 @@ public class ScanActivity extends LFScanProcessing implements GoogleApiClient.Co
97
         setStatusScan();
109
         setStatusScan();
98
         locationClient = LocationServices.getFusedLocationProviderClient(this);
110
         locationClient = LocationServices.getFusedLocationProviderClient(this);
99
         getLocation();
111
         getLocation();
112
+        getSimpleLocation();
100
         binding.toggleScan.setOnCheckedChangeListener((buttonView, isChecked) -> statusScanner());
113
         binding.toggleScan.setOnCheckedChangeListener((buttonView, isChecked) -> statusScanner());
101
     }
114
     }
102
 
115
 
@@ -105,6 +118,9 @@ public class ScanActivity extends LFScanProcessing implements GoogleApiClient.Co
105
         super.onResume();
118
         super.onResume();
106
         //Now lets connect to the API
119
         //Now lets connect to the API
107
         mGoogleApiClient.connect();
120
         mGoogleApiClient.connect();
121
+
122
+        // make the device update its location
123
+        simpleLocation.beginUpdates();
108
     }
124
     }
109
 
125
 
110
     @Override
126
     @Override
@@ -115,6 +131,9 @@ public class ScanActivity extends LFScanProcessing implements GoogleApiClient.Co
115
             LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
131
             LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
116
             mGoogleApiClient.disconnect();
132
             mGoogleApiClient.disconnect();
117
         }
133
         }
134
+
135
+        // stop location updates (saves battery)
136
+        simpleLocation.endUpdates();
118
     }
137
     }
119
 
138
 
120
     private void setStatusScan() {
139
     private void setStatusScan() {
@@ -190,6 +209,13 @@ public class ScanActivity extends LFScanProcessing implements GoogleApiClient.Co
190
         locationClient.getLastLocation().addOnFailureListener(e -> System.out.println(e.getMessage()));
209
         locationClient.getLastLocation().addOnFailureListener(e -> System.out.println(e.getMessage()));
191
     }
210
     }
192
 
211
 
212
+    private void getSimpleLocation(){
213
+        double latitude = simpleLocation.getLatitude();
214
+        double longitude = simpleLocation.getLongitude();
215
+        longLat = longitude + "," + latitude;
216
+        Toast.makeText(this, "Simple Location ==> Latitude : " + latitude + ", Longitude : " + longitude, Toast.LENGTH_LONG).show();
217
+    }
218
+
193
     @Override
219
     @Override
194
     protected void onDestroy() {
220
     protected void onDestroy() {
195
         super.onDestroy();
221
         super.onDestroy();