|
@@ -49,6 +49,8 @@ public abstract class RAService extends Service {
|
|
|
protected static String CHANNEL_ID = "RAService";
|
|
protected static String CHANNEL_ID = "RAService";
|
|
|
protected static String CHANNEL_NAME = "RAService";
|
|
protected static String CHANNEL_NAME = "RAService";
|
|
|
protected static String LAUNCH_MSG = "SERVICE IS RUNNING";
|
|
protected static String LAUNCH_MSG = "SERVICE IS RUNNING";
|
|
|
|
|
+ public static int FOREGROUND_ICON = R.drawable.ic_launcher_foreground;
|
|
|
|
|
+ public static int BACKGROUND_ICON = R.drawable.ic_launcher_background;
|
|
|
public static final int DEFAULT_LOCATION_TIMEINTERVAL = 30 * 1000;
|
|
public static final int DEFAULT_LOCATION_TIMEINTERVAL = 30 * 1000;
|
|
|
// public static final int DEFAULT_DISTANCE = 0 * 50;
|
|
// public static final int DEFAULT_DISTANCE = 0 * 50;
|
|
|
public static final int DEFAULT_PUSHNOTIFICATION_TIMEINTERVAL = 30 * 1000;
|
|
public static final int DEFAULT_PUSHNOTIFICATION_TIMEINTERVAL = 30 * 1000;
|
|
@@ -62,6 +64,8 @@ public abstract class RAService extends Service {
|
|
|
|
|
|
|
|
private static boolean System_Location_Flag = false;
|
|
private static boolean System_Location_Flag = false;
|
|
|
|
|
|
|
|
|
|
+ protected static boolean backgroundLocationTracing = false;
|
|
|
|
|
+
|
|
|
private Location mCurrentLocation; // 当前定位位置,可为空
|
|
private Location mCurrentLocation; // 当前定位位置,可为空
|
|
|
private Location mLastNotNullLocation; // 最后一次获取的非空Location
|
|
private Location mLastNotNullLocation; // 最后一次获取的非空Location
|
|
|
|
|
|
|
@@ -173,6 +177,13 @@ public abstract class RAService extends Service {
|
|
|
// Utils.setRequestingLocationUpdates(this, true);
|
|
// Utils.setRequestingLocationUpdates(this, true);
|
|
|
// startService(new Intent(getApplicationContext(), LocationUpdatesService.class));
|
|
// startService(new Intent(getApplicationContext(), LocationUpdatesService.class));
|
|
|
try {
|
|
try {
|
|
|
|
|
+
|
|
|
|
|
+ Log.d(TAG, "requestLocationUpdates: ");
|
|
|
|
|
+// long dbg_l = mLocationRequest.getNumUpdates();
|
|
|
|
|
+// if(backgroundLocationTracing)
|
|
|
|
|
+// mLocationRequest.setNumUpdates(0);
|
|
|
|
|
+// else
|
|
|
|
|
+// mLocationRequest.setNumUpdates(1);
|
|
|
mFusedLocationClient.requestLocationUpdates(mLocationRequest,
|
|
mFusedLocationClient.requestLocationUpdates(mLocationRequest,
|
|
|
mLocationCallback, null);
|
|
mLocationCallback, null);
|
|
|
} catch (SecurityException unlikely) {
|
|
} catch (SecurityException unlikely) {
|
|
@@ -617,7 +628,7 @@ public abstract class RAService extends Service {
|
|
|
Notification notification = new NotificationCompat.Builder(this, "Service Start")
|
|
Notification notification = new NotificationCompat.Builder(this, "Service Start")
|
|
|
// .setContentTitle("Apex Land is running.")
|
|
// .setContentTitle("Apex Land is running.")
|
|
|
.setContentText(LAUNCH_MSG)
|
|
.setContentText(LAUNCH_MSG)
|
|
|
- .setCategory(Notification.CATEGORY_SERVICE).setSmallIcon(R.drawable.ic_launcher_foreground).setPriority(1000).build();
|
|
|
|
|
|
|
+ .setCategory(Notification.CATEGORY_SERVICE).setSmallIcon(FOREGROUND_ICON).setPriority(1000).build();
|
|
|
|
|
|
|
|
startForeground(101, notification);
|
|
startForeground(101, notification);
|
|
|
}
|
|
}
|
|
@@ -674,6 +685,7 @@ public abstract class RAService extends Service {
|
|
|
|
|
|
|
|
if(mFusedLocationClient!=null)
|
|
if(mFusedLocationClient!=null)
|
|
|
return;
|
|
return;
|
|
|
|
|
+ Log.d(TAG, "initLocation: ");
|
|
|
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
|
|
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
|
|
|
|
|
|
|
|
mLocationCallback = new LocationCallback() {
|
|
mLocationCallback = new LocationCallback() {
|
|
@@ -681,7 +693,7 @@ public abstract class RAService extends Service {
|
|
|
public void onLocationResult(LocationResult locationResult) {
|
|
public void onLocationResult(LocationResult locationResult) {
|
|
|
super.onLocationResult(locationResult);
|
|
super.onLocationResult(locationResult);
|
|
|
|
|
|
|
|
- Log.i(TAG, "onLocationResult: " + locationResult.getLastLocation());
|
|
|
|
|
|
|
+ Log.i(TAG, "FusedLocationClient onLocationResult: " + locationResult.getLastLocation());
|
|
|
// onNewLocation(locationResult.getLastLocation());
|
|
// onNewLocation(locationResult.getLastLocation());
|
|
|
|
|
|
|
|
Location location = locationResult.getLastLocation();
|
|
Location location = locationResult.getLastLocation();
|
|
@@ -747,13 +759,16 @@ public abstract class RAService extends Service {
|
|
|
reachabilityCallback.networkStateChanged(networkInfo);
|
|
reachabilityCallback.networkStateChanged(networkInfo);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- } else if (RABroadcast.ACTION_LOCATION_ENABLE_TRACING.equals(action)) {
|
|
|
|
|
|
|
+ } else
|
|
|
|
|
+ if (RABroadcast.ACTION_LOCATION_ENABLE_TRACING.equals(action)) {
|
|
|
enable_locationTracing();
|
|
enable_locationTracing();
|
|
|
- } else if (RABroadcast.ACTION_LOCATION_DISABLE_TRACING
|
|
|
|
|
|
|
+ } else
|
|
|
|
|
+ if (RABroadcast.ACTION_LOCATION_DISABLE_TRACING
|
|
|
.equals(action)) {
|
|
.equals(action)) {
|
|
|
|
|
|
|
|
disable_locationTracing();
|
|
disable_locationTracing();
|
|
|
- } else if (RABroadcast.ACTION_LOCATION_REQUEST_LOCATION
|
|
|
|
|
|
|
+ } else
|
|
|
|
|
+ if (RABroadcast.ACTION_LOCATION_REQUEST_LOCATION
|
|
|
.equals(action)) {
|
|
.equals(action)) {
|
|
|
|
|
|
|
|
|
|
|
|
@@ -882,6 +897,8 @@ public abstract class RAService extends Service {
|
|
|
|
|
|
|
|
void disable_locationTracing() {
|
|
void disable_locationTracing() {
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+ backgroundLocationTracing = false;
|
|
|
if (System_Location_Flag) {
|
|
if (System_Location_Flag) {
|
|
|
|
|
|
|
|
removeLocationUpdates();
|
|
removeLocationUpdates();
|
|
@@ -908,12 +925,14 @@ public abstract class RAService extends Service {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ backgroundLocationTracing = true;
|
|
|
if (System_Location_Flag) {
|
|
if (System_Location_Flag) {
|
|
|
|
|
|
|
|
initLocation();
|
|
initLocation();
|
|
|
|
|
|
|
|
} else {
|
|
} else {
|
|
|
|
|
|
|
|
|
|
+ Log.d(TAG, "enable_locationTracing: ");
|
|
|
initLocation();
|
|
initLocation();
|
|
|
requestLocationUpdates();
|
|
requestLocationUpdates();
|
|
|
// locationManager.requestLocationUpdates(
|
|
// locationManager.requestLocationUpdates(
|
|
@@ -954,24 +973,10 @@ public abstract class RAService extends Service {
|
|
|
|
|
|
|
|
protected void request_location(final String receiverID) {
|
|
protected void request_location(final String receiverID) {
|
|
|
|
|
|
|
|
- if (System_Location_Flag) {
|
|
|
|
|
-
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
if (receiverID == null)
|
|
if (receiverID == null)
|
|
|
throw new IllegalArgumentException("receiverID can't be null");
|
|
throw new IllegalArgumentException("receiverID can't be null");
|
|
|
|
|
|
|
|
-// Criteria criteria = new Criteria();
|
|
|
|
|
-// criteria.setAccuracy(Criteria.ACCURACY_COARSE);//低精度,如果设置为高精度,依然获取不了location。
|
|
|
|
|
-// criteria.setAltitudeRequired(false);//不要求海拔
|
|
|
|
|
-// criteria.setBearingRequired(false);//不要求方位
|
|
|
|
|
-// criteria.setCostAllowed(true);//允许有花费
|
|
|
|
|
-// criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗
|
|
|
|
|
-//
|
|
|
|
|
-// //从可用的位置提供器中,匹配以上标准的最佳提供器
|
|
|
|
|
-// String locationProvider = locationManager.getBestProvider(criteria, true);
|
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
|
|
// TODO: Consider calling
|
|
// TODO: Consider calling
|
|
@@ -984,44 +989,34 @@ public abstract class RAService extends Service {
|
|
|
return;
|
|
return;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return;
|
|
|
|
|
-// LocationListener singleListener = new LocationListener() {
|
|
|
|
|
-// @Override
|
|
|
|
|
-// public void onLocationChanged(Location location) {
|
|
|
|
|
-//
|
|
|
|
|
-//
|
|
|
|
|
-//
|
|
|
|
|
-// if(location==null)
|
|
|
|
|
-// Log.d("ApexDriversB..Service", "onLocationChanged: null");
|
|
|
|
|
-// else
|
|
|
|
|
-// Log.d("ApexDriversB..Service", "single location onLocationChanged: "+location.getLongitude()+" , "+location.getLatitude());
|
|
|
|
|
-// Intent Bintent = new Intent(RABroadcast.EVENT_RETURN_LOCATION);
|
|
|
|
|
-// Bintent.putExtra("location", location);
|
|
|
|
|
-// Bintent.putExtra("receiverID", receiverID);
|
|
|
|
|
-// sendBroadcast(Bintent);
|
|
|
|
|
-//
|
|
|
|
|
-//
|
|
|
|
|
-// }
|
|
|
|
|
-//
|
|
|
|
|
-// @Override
|
|
|
|
|
-// public void onStatusChanged(String s, int i, Bundle bundle) {
|
|
|
|
|
|
|
+ backgroundLocationTracing = true;
|
|
|
|
|
+ if (System_Location_Flag) {
|
|
|
|
|
+
|
|
|
|
|
+ initLocation();
|
|
|
|
|
+
|
|
|
|
|
+ } else {
|
|
|
|
|
+
|
|
|
|
|
+ initLocation();
|
|
|
|
|
+ requestLocationUpdates();
|
|
|
|
|
+// locationManager.requestLocationUpdates(
|
|
|
|
|
+// "fused", timeInterval, distance,
|
|
|
|
|
+// locationListener);
|
|
|
|
|
+// if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
|
|
//
|
|
//
|
|
|
-// }
|
|
|
|
|
//
|
|
//
|
|
|
-// @Override
|
|
|
|
|
-// public void onProviderEnabled(String s) {
|
|
|
|
|
//
|
|
//
|
|
|
-// }
|
|
|
|
|
//
|
|
//
|
|
|
-// @Override
|
|
|
|
|
-// public void onProviderDisabled(String s) {
|
|
|
|
|
|
|
+// } else if (locationManager
|
|
|
|
|
+// .isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
|
|
|
//
|
|
//
|
|
|
-// }
|
|
|
|
|
-// };
|
|
|
|
|
//
|
|
//
|
|
|
|
|
+// locationManager.requestLocationUpdates(
|
|
|
|
|
+// LocationManager.NETWORK_PROVIDER, timeInterval, distance,
|
|
|
|
|
+// locationListener);
|
|
|
//
|
|
//
|
|
|
-// locationManager.requestSingleUpdate("fused", singleListener, null);
|
|
|
|
|
|
|
+// }
|
|
|
|
|
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|