|
|
@@ -12,8 +12,6 @@ import android.content.IntentFilter;
|
|
|
import android.content.SharedPreferences;
|
|
|
import android.content.pm.PackageManager;
|
|
|
import android.location.Location;
|
|
|
-import android.location.LocationListener;
|
|
|
-import android.location.LocationManager;
|
|
|
import android.net.ConnectivityManager;
|
|
|
import android.net.NetworkInfo;
|
|
|
import android.os.AsyncTask;
|
|
|
@@ -21,16 +19,25 @@ import android.os.Binder;
|
|
|
import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
|
import android.os.IBinder;
|
|
|
+import android.support.annotation.NonNull;
|
|
|
import android.support.v4.app.ActivityCompat;
|
|
|
import android.support.v4.app.NotificationCompat;
|
|
|
import android.text.TextUtils;
|
|
|
import android.util.Log;
|
|
|
|
|
|
+import com.google.android.gms.location.FusedLocationProviderClient;
|
|
|
+import com.google.android.gms.location.LocationCallback;
|
|
|
+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.OnCompleteListener;
|
|
|
+import com.google.android.gms.tasks.Task;
|
|
|
import com.usai.redant.rautils.R;
|
|
|
import com.usai.redant.rautils.receiver.RABroadcast;
|
|
|
import com.usai.redant.rautils.receiver.RABroadcastReceiver;
|
|
|
import com.usai.redant.rautils.upload.RAUploadManager;
|
|
|
import com.usai.redant.rautils.utils.Network;
|
|
|
+import com.usai.redant.rautils.utils.RAUtil;
|
|
|
import com.usai.redant.rautils.utils.dbgUtil;
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
@@ -43,9 +50,9 @@ public abstract class RAService extends Service {
|
|
|
|
|
|
private static final String TAG = "RAService";
|
|
|
|
|
|
- public static final int DEFAULT_LOCATION_TIMEINTERVAL = 60 * 1000;
|
|
|
- public static final int DEFAULT_DISTANCE = 60 * 50;
|
|
|
- public static final int DEFAULT_PUSHNOTIFICATION_TIMEINTERVAL = 5 * 1000;
|
|
|
+ public static final int DEFAULT_LOCATION_TIMEINTERVAL = 30 * 1000;
|
|
|
+// public static final int DEFAULT_DISTANCE = 0 * 50;
|
|
|
+ public static final int DEFAULT_PUSHNOTIFICATION_TIMEINTERVAL = 30 * 1000;
|
|
|
|
|
|
public static final int FLAG_SERVICE_NONE = 0;
|
|
|
public static final int FLAG_SERVICE_LOCATION = 1 << 1;
|
|
|
@@ -76,10 +83,30 @@ public abstract class RAService extends Service {
|
|
|
}
|
|
|
|
|
|
// sub function location
|
|
|
- LocationListener locationListener = null;
|
|
|
- LocationManager locationManager = null;
|
|
|
- int locationTracing_timeInterval = DEFAULT_LOCATION_TIMEINTERVAL;
|
|
|
- int locationTracing_distance = DEFAULT_DISTANCE;
|
|
|
+// LocationListener locationListener = null;
|
|
|
+// LocationManager locationManager = null;
|
|
|
+
|
|
|
+ private LocationRequest mLocationRequest;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Provides access to the Fused Location Provider API.
|
|
|
+ */
|
|
|
+ private FusedLocationProviderClient mFusedLocationClient;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Callback for changes in location.
|
|
|
+ */
|
|
|
+ private LocationCallback mLocationCallback;
|
|
|
+
|
|
|
+// private Handler mServiceHandler;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The current location.
|
|
|
+ */
|
|
|
+ private Location mLocation;
|
|
|
+
|
|
|
+// int locationTracing_timeInterval = DEFAULT_LOCATION_TIMEINTERVAL;
|
|
|
+// int locationTracing_distance = DEFAULT_DISTANCE;
|
|
|
// protected abstract void onLocationChanged(Location location);
|
|
|
private ServiceLocation locationCallback = null;
|
|
|
|
|
|
@@ -98,8 +125,62 @@ public abstract class RAService extends Service {
|
|
|
service_flag = service_flag | FLAG_SERVICE_LOCATION;
|
|
|
this.locationCallback = locationCallback;
|
|
|
}
|
|
|
+ /**
|
|
|
+ * Sets the location request parameters.
|
|
|
+ */
|
|
|
+ private void createLocationRequest() {
|
|
|
+ mLocationRequest = new LocationRequest();
|
|
|
+ mLocationRequest.setInterval(DEFAULT_LOCATION_TIMEINTERVAL);
|
|
|
+ mLocationRequest.setFastestInterval(DEFAULT_LOCATION_TIMEINTERVAL/2);
|
|
|
+ mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
|
|
|
|
|
|
+ }
|
|
|
+
|
|
|
+ private void requestLocationUpdates() {
|
|
|
+ Log.i(TAG, "Requesting location updates");
|
|
|
+// Utils.setRequestingLocationUpdates(this, true);
|
|
|
+// startService(new Intent(getApplicationContext(), LocationUpdatesService.class));
|
|
|
+ try {
|
|
|
+ mFusedLocationClient.requestLocationUpdates(mLocationRequest,
|
|
|
+ mLocationCallback, null);
|
|
|
+ } catch (SecurityException unlikely) {
|
|
|
+// Utils.setRequestingLocationUpdates(this, false);
|
|
|
+ Log.e(TAG, "Lost location permission. Could not request updates. " + unlikely);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ /**
|
|
|
+ * Removes location updates. Note that in this sample we merely log the
|
|
|
+ * {@link SecurityException}.
|
|
|
+ */
|
|
|
+ private void removeLocationUpdates() {
|
|
|
+ Log.i(TAG, "Removing location updates");
|
|
|
+ try {
|
|
|
+ mFusedLocationClient.removeLocationUpdates(mLocationCallback);
|
|
|
+// Utils.setRequestingLocationUpdates(this, false);
|
|
|
+ stopSelf();
|
|
|
+ } catch (SecurityException unlikely) {
|
|
|
+// Utils.setRequestingLocationUpdates(this, true);
|
|
|
+ Log.e(TAG, "Lost location permission. Could not remove updates. " + unlikely);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ private void getLastLocation() {
|
|
|
+ try {
|
|
|
+ mFusedLocationClient.getLastLocation()
|
|
|
+ .addOnCompleteListener(new OnCompleteListener<Location>() {
|
|
|
+ @Override
|
|
|
+ public void onComplete(@NonNull Task<Location> task) {
|
|
|
+ if (task.isSuccessful() && task.getResult() != null) {
|
|
|
+ mLocation = task.getResult();
|
|
|
+ } else {
|
|
|
+ Log.w(TAG, "Failed to get location.");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } catch (SecurityException unlikely) {
|
|
|
+ Log.e(TAG, "Lost location permission." + unlikely);
|
|
|
+ }
|
|
|
+ }
|
|
|
// sub function Notification
|
|
|
protected String url_checknotification = null;
|
|
|
int pushcheck_timeInterval = DEFAULT_PUSHNOTIFICATION_TIMEINTERVAL;
|
|
|
@@ -287,24 +368,7 @@ public abstract class RAService extends Service {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public void checkpush(Context context) {
|
|
|
-
|
|
|
- if(true)
|
|
|
- return;
|
|
|
- if (m_task != null) {
|
|
|
- return;
|
|
|
- }
|
|
|
- // mStatusMessageView.setText(R.string.str_Loading);
|
|
|
- // showProgress(true);
|
|
|
- m_task = new checkPushTask(context);
|
|
|
-
|
|
|
- // TextView text_page = (TextView) view_page_footer
|
|
|
- // .findViewById(R.id.text_page);
|
|
|
- // text_page.setText("Loading...");
|
|
|
- // text_page.setEnabled(false);
|
|
|
- m_task.execute();
|
|
|
-
|
|
|
- }
|
|
|
+//
|
|
|
|
|
|
public RAService() {
|
|
|
}
|
|
|
@@ -329,7 +393,11 @@ public abstract class RAService extends Service {
|
|
|
return RAService.this;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+ @Override
|
|
|
+ public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
+ Log.d("_SERVICE", "onStartCommand");
|
|
|
+ return super.onStartCommand(intent,flags,startId);
|
|
|
+ }
|
|
|
|
|
|
@Override
|
|
|
public void onCreate() {
|
|
|
@@ -380,39 +448,59 @@ public abstract class RAService extends Service {
|
|
|
msgFilter.addAction(RABroadcast.ACTION_LOCATION_DISABLE_TRACING);
|
|
|
msgFilter.addAction(RABroadcast.ACTION_LOCATION_REQUEST_LOCATION);
|
|
|
// msgFilter.addAction("REDANT.BROADCAST.RESET_LOCATION");
|
|
|
- locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
|
|
|
-
|
|
|
- locationListener = new LocationListener() {
|
|
|
-
|
|
|
- // Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
|
|
|
- @Override
|
|
|
- public void onStatusChanged(String provider, int status,
|
|
|
- Bundle extras) {
|
|
|
+// locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
|
|
|
+//
|
|
|
+// locationListener = new LocationListener() {
|
|
|
+//
|
|
|
+// // Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
|
|
|
+// @Override
|
|
|
+// public void onStatusChanged(String provider, int status,
|
|
|
+// Bundle extras) {
|
|
|
+//
|
|
|
+// }
|
|
|
+//
|
|
|
+// // Provider被enable时触发此函数,比如GPS被打开
|
|
|
+// @Override
|
|
|
+// public void onProviderEnabled(String provider) {
|
|
|
+//
|
|
|
+// }
|
|
|
+//
|
|
|
+// // Provider被disable时触发此函数,比如GPS被关闭
|
|
|
+// @Override
|
|
|
+// public void onProviderDisabled(String provider) {
|
|
|
+//
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
|
|
|
+// @Override
|
|
|
+// public void onLocationChanged(Location location) {
|
|
|
+// locationCallback.onLocationChanged(location);
|
|
|
+//
|
|
|
+// }
|
|
|
+// };
|
|
|
|
|
|
- }
|
|
|
|
|
|
- // Provider被enable时触发此函数,比如GPS被打开
|
|
|
- @Override
|
|
|
- public void onProviderEnabled(String provider) {
|
|
|
|
|
|
- }
|
|
|
+ mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
|
|
|
|
|
|
- // Provider被disable时触发此函数,比如GPS被关闭
|
|
|
+ mLocationCallback = new LocationCallback() {
|
|
|
@Override
|
|
|
- public void onProviderDisabled(String provider) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- // 当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
|
|
|
- @Override
|
|
|
- public void onLocationChanged(Location location) {
|
|
|
- locationCallback.onLocationChanged(location);
|
|
|
+ public void onLocationResult(LocationResult locationResult) {
|
|
|
+ super.onLocationResult(locationResult);
|
|
|
|
|
|
+ Log.i(TAG, "onLocationResult: " + locationResult.getLastLocation());
|
|
|
+// onNewLocation(locationResult.getLastLocation());
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+ createLocationRequest();
|
|
|
+ getLastLocation();
|
|
|
+
|
|
|
+// HandlerThread handlerThread = new HandlerThread(TAG);
|
|
|
+// handlerThread.start();
|
|
|
+// mServiceHandler = new Handler(handlerThread.getLooper());
|
|
|
|
|
|
- enable_locationTracing(locationTracing_timeInterval, locationTracing_distance);
|
|
|
+ enable_locationTracing();
|
|
|
|
|
|
|
|
|
// msgFilter.addAction("REDANT.POP.GPS_ON");
|
|
|
@@ -460,9 +548,10 @@ public abstract class RAService extends Service {
|
|
|
@Override
|
|
|
public void onDestroy() {
|
|
|
Log.e("_SERVICE", "onDestroy: ");
|
|
|
- if (uploadManager != null)
|
|
|
- uploadManager.saveTasks();
|
|
|
-
|
|
|
+// if (uploadManager != null)
|
|
|
+// uploadManager.saveTasks();
|
|
|
+// if(mServiceHandler!=null)
|
|
|
+// mServiceHandler.removeCallbacksAndMessages(null);
|
|
|
unregisterReceiver(uploadReceiver);
|
|
|
super.onDestroy();
|
|
|
}
|
|
|
@@ -515,10 +604,10 @@ public abstract class RAService extends Service {
|
|
|
int iconntype = -1;
|
|
|
iconntype = networkInfo
|
|
|
.getType();
|
|
|
- SharedPreferences pref = getApplication()
|
|
|
- .getSharedPreferences(
|
|
|
- "UploadManager",
|
|
|
- 0);
|
|
|
+
|
|
|
+
|
|
|
+ SharedPreferences pref = RAUtil.sharedPreferences(getApplicationContext(),"UploadManager");
|
|
|
+
|
|
|
|
|
|
|
|
|
boolean wifi_only = pref.getBoolean("wifi_only", false);
|
|
|
@@ -536,7 +625,7 @@ public abstract class RAService extends Service {
|
|
|
|
|
|
|
|
|
} else if (RABroadcast.ACTION_LOCATION_ENABLE_TRACING.equals(action)) {
|
|
|
- enable_locationTracing(locationTracing_timeInterval, locationTracing_distance);
|
|
|
+ enable_locationTracing();
|
|
|
} else if (RABroadcast.ACTION_LOCATION_DISABLE_TRACING
|
|
|
.equals(action)) {
|
|
|
|
|
|
@@ -669,11 +758,12 @@ public abstract class RAService extends Service {
|
|
|
|
|
|
|
|
|
void disable_locationTracing() {
|
|
|
- locationManager.removeUpdates(locationListener);
|
|
|
+ removeLocationUpdates();
|
|
|
+// locationManager.removeUpdates(locationListener);
|
|
|
}
|
|
|
|
|
|
|
|
|
- void enable_locationTracing(int timeInterval, int distance) {
|
|
|
+ void enable_locationTracing() {
|
|
|
|
|
|
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
|
|
|
@@ -686,10 +776,10 @@ public abstract class RAService extends Service {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- locationManager.requestLocationUpdates(
|
|
|
- "fused", timeInterval, distance,
|
|
|
- locationListener);
|
|
|
+ requestLocationUpdates();
|
|
|
+// locationManager.requestLocationUpdates(
|
|
|
+// "fused", timeInterval, distance,
|
|
|
+// locationListener);
|
|
|
// if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
|
|
|
//
|
|
|
//
|
|
|
@@ -714,8 +804,9 @@ public abstract class RAService extends Service {
|
|
|
|
|
|
|
|
|
//使用GPS获取上一次的地址,这样获取到的信息需要多次,才能够显示出来,所以后面有动态的判断
|
|
|
- Location location = locationManager.getLastKnownLocation("fused");
|
|
|
- return location;
|
|
|
+// Location location = locationManager.getLastKnownLocation("fused");
|
|
|
+// return location;
|
|
|
+ return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
@@ -745,36 +836,43 @@ public abstract class RAService extends Service {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- LocationListener singleListener = new LocationListener() {
|
|
|
- @Override
|
|
|
- public void onLocationChanged(Location location) {
|
|
|
-
|
|
|
- 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) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onProviderEnabled(String s) {
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onProviderDisabled(String s) {
|
|
|
-
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
-
|
|
|
- locationManager.requestSingleUpdate("fused", singleListener, null);
|
|
|
+ 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) {
|
|
|
+//
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void onProviderEnabled(String s) {
|
|
|
+//
|
|
|
+// }
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public void onProviderDisabled(String s) {
|
|
|
+//
|
|
|
+// }
|
|
|
+// };
|
|
|
+//
|
|
|
+//
|
|
|
+// locationManager.requestSingleUpdate("fused", singleListener, null);
|
|
|
|
|
|
|
|
|
}
|