我正在构建一个应用程序,我正在使用位置服务。我在MainActivity上的onCreate中启动该服务,但如果GPS关闭,我的服务将不会查找经度和纬度。即使我在那之后打开GPS,我的服务也不会看起来很长。只有在我开始服务(和MainActivity)之前打开GPS,服务才能正常工作,它会寻找位置。当我在应用程序中花费一段时间后打开GPS时,我应该更改以设置我的服务不会出错? 这是我的位置服务:
public class LocationUpdateService extends Service implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
protected static final String TAG = "LocationUpdateService";
public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 30000;
public static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2;
public static Boolean mRequestingLocationUpdates;
protected String mLastUpdateTime;
protected String timeWhenStartLocationUpdates;
protected String timeWhenStopLocationUpdates;
protected String timeWhenServiceDestroyed;
protected String connectionFailed;
protected GoogleApiClient mGoogleApiClient;
protected LocationRequest mLocationRequest;
protected Location mCurrentLocation;
protected String timeWhenGetInGetLatLong;
protected String report_time;
protected String timeFromElse;
public static boolean isEnded = false;
Calendar calendar;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onConnected(@Nullable Bundle bundle) {
startLocationUpdates();
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "Connection suspended==");
mGoogleApiClient.connect();
}
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
Log.d("mCurrentLocation", "Location is: " + location);
getLatLong();
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_location", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("TimeWhenCameToOnLocationChanged", mLastUpdateTime);
editor.apply();
Log.d("onLocationchanged", "OnLocationChangedInServiceLocation");
Log.d("Time ", "Time of lat and long from Service: " + mLastUpdateTime);
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
connectionFailed = DateFormat.getTimeInstance().format(new Date());
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_location", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("connectionFailed", connectionFailed);
editor.apply();
Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + connectionResult.getErrorCode());
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("LOC", "Service init...");
return Service.START_STICKY;
}
protected synchronized void buildGoogleApiClient() {
Log.i(TAG, "Building GoogleApiClient===");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
createLocationRequest();
}
public void getLatLong() {
timeWhenGetInGetLatLong = DateFormat.getTimeInstance().format(new Date());
if (mCurrentLocation != null && mLastUpdateTime != null) {
String lat = String.valueOf(mCurrentLocation.getLatitude());
String lng = String.valueOf(mCurrentLocation.getLongitude());
calendar = Calendar.getInstance();
@SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
report_time = simpleDateFormat.format(calendar.getTime());
Log.d("preferences", "Time stored in preferences: " + mLastUpdateTime);
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_location", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("user_latitude", lat);
editor.putString("user_longitude", lng);
editor.putString("getLatLongIf", "ok");
editor.putString("report_time_for_location_response", report_time);
editor.putString("time_when_comes_to_getLatLong", timeWhenGetInGetLatLong);
editor.apply();
}else{
timeFromElse = DateFormat.getTimeInstance().format(new Date());
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_location", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("getLatLongElse", timeFromElse);
editor.apply();
}
}
protected void createLocationRequest() {
mGoogleApiClient.connect();
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
protected void startLocationUpdates() {
timeWhenStartLocationUpdates = DateFormat.getTimeInstance().format(new Date());
if (!mRequestingLocationUpdates) {
mRequestingLocationUpdates = true;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_location", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("startLocationUpdates", timeWhenStartLocationUpdates);
editor.apply();
Log.i("startUpdates", " startLocationUpdates===" + timeWhenStartLocationUpdates);
isEnded = true;
}
}
protected void stopLocationUpdates() {
if (mRequestingLocationUpdates) {
mRequestingLocationUpdates = false;
timeWhenStopLocationUpdates = DateFormat.getTimeInstance().format(new Date());
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_location", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("stopLocationUpdates", timeWhenStopLocationUpdates);
editor.apply();
Log.d("stopUpdates", "stopLocationUpdates();==" + timeWhenStopLocationUpdates);
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
@Override
public void onDestroy() {
super.onDestroy();
timeWhenServiceDestroyed = DateFormat.getTimeInstance().format(new Date());
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_location", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("onDestroyService", timeWhenServiceDestroyed);
editor.apply();
stopLocationUpdates();
Log.d("onDestroyService", "onDestroy: + " + timeWhenServiceDestroyed);
}
@Override
public void onCreate() {
super.onCreate();
isEnded = false;
mRequestingLocationUpdates = false;
mLastUpdateTime = "";
buildGoogleApiClient();
Log.d("onCreateService", "onCreateService");
}
再一次,我在主要活动的onCreate上开始我的服务...... 谁能帮助我?提前谢谢。
答案 0 :(得分:0)
请检查我如何更改您的代码服务。
步骤1.制作此课程 GoogleLocationService.java 强>
public class GoogleLocationService {
private GoogleServicesCallbacks callbacks = new GoogleServicesCallbacks();
LocationUpdateListener locationUpdateListener;
Context activity;
protected GoogleApiClient mGoogleApiClient;
protected LocationRequest mLocationRequest;
public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 30000;
public GoogleLocationService(Context activity, LocationUpdateListener locationUpdateListener) {
this.locationUpdateListener = locationUpdateListener;
this.activity = activity;
buildGoogleApiClient();
}
protected synchronized void buildGoogleApiClient() {
//Log.i(TAG, "Building GoogleApiClient");
mGoogleApiClient = new GoogleApiClient.Builder(activity)
.addConnectionCallbacks(callbacks)
.addOnConnectionFailedListener(callbacks)
.addApi(LocationServices.API)
.build();
createLocationRequest();
mGoogleApiClient.connect();
}
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
private class GoogleServicesCallbacks implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
@Override
public void onConnected(Bundle bundle) {
startLocationUpdates();
}
@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
if (connectionResult.getErrorCode() == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED) {
Toast.makeText(activity, "Google play service not updated", Toast.LENGTH_LONG).show();
String connectionFailed = DateFormat.getTimeInstance().format(new Date());
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_location", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("connectionFailed", connectionFailed);
editor.apply();
Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + connectionResult.getErrorCode());
}
locationUpdateListener.cannotReceiveLocationUpdates();
}
@Override
public void onLocationChanged(Location location) {
if (location.hasAccuracy()) {
if (location.getAccuracy() < 30) {
locationUpdateListener.updateLocation(location);
}
}
}
}
private static boolean locationEnabled(Context context) {
boolean gps_enabled = false;
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
try {
gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
} catch (Exception ex) {
ex.printStackTrace();
}
return gps_enabled;
}
private boolean servicesConnected(Context context) {
return isPackageInstalled(GooglePlayServicesUtil.GOOGLE_PLAY_STORE_PACKAGE, context);
}
private boolean isPackageInstalled(String packagename, Context context) {
PackageManager pm = context.getPackageManager();
try {
pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return false;
}
}
public void startUpdates() {
/*
* Connect the client. Don't re-start any requests here; instead, wait
* for onResume()
*/
if (servicesConnected(activity)) {
if (locationEnabled(activity)) {
locationUpdateListener.canReceiveLocationUpdates();
startLocationUpdates();
} else {
locationUpdateListener.cannotReceiveLocationUpdates();
Toast.makeText(activity, "Unable to get your location.Please turn on your device Gps", Toast.LENGTH_LONG).show();
}
} else {
locationUpdateListener.cannotReceiveLocationUpdates();
Toast.makeText(activity, "Google play service not available", Toast.LENGTH_LONG).show();
}
}
//stop location updates
public void stopUpdates() {
stopLocationUpdates();
}
//start location updates
private void startLocationUpdates() {
if (checkSelfPermission(activity, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(activity, ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, callbacks);
String timeWhenStartLocationUpdates = DateFormat.getTimeInstance().format(new Date());
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_location", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("startLocationUpdates", timeWhenStartLocationUpdates);
editor.apply();
}
}
public void stopLocationUpdates() {
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, callbacks);
String timeWhenStopLocationUpdates = DateFormat.getTimeInstance().format(new Date());
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_location", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("stopLocationUpdates", timeWhenStopLocationUpdates);
editor.apply();
}
}
public void startGoogleApi() {
mGoogleApiClient.connect();
}
public void closeGoogleApi() {
mGoogleApiClient.disconnect();
}
}
<强>第二步。制作这个界面 LocationUpdateListener.java 强>
public interface LocationUpdateListener {
/**
* Called immediately the service starts if the service can obtain location
*/
void canReceiveLocationUpdates();
/**
* Called immediately the service tries to start if it cannot obtain location - eg the user has disabled wireless and
*/
void cannotReceiveLocationUpdates();
/**
* Called whenever the location has changed (at least non-trivially)
* @param location
*/
void updateLocation(Location location);
/**
* Called when GoogleLocationServices detects that the device has moved to a new location.
* @param localityName The name of the locality (somewhere below street but above area).
*/
void updateLocationName(String localityName, Location location);
}
步骤3.制作此位置服务类 LocationUpdateService.java 强>
public class LocationUpdateService extends Service {
protected String mLastUpdateTime;
protected String timeWhenServiceDestroyed;
protected String timeWhenGetInGetLatLong;
protected String report_time;
protected String timeFromElse;
Calendar calendar;
private GoogleLocationService googleLocationService;
IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder {
public LocationUpdateService getServerInstance() {
return LocationUpdateService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("LOC", "Service init...");
return Service.START_STICKY;
}
protected synchronized void buildGoogleApiClient() {
Log.i(TAG, "Building GoogleApiClient===");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
createLocationRequest();
}
public void getLatLong() {
timeWhenGetInGetLatLong = DateFormat.getTimeInstance().format(new Date());
if (mCurrentLocation != null && mLastUpdateTime != null) {
String lat = String.valueOf(mCurrentLocation.getLatitude());
String lng = String.valueOf(mCurrentLocation.getLongitude());
calendar = Calendar.getInstance();
@SuppressLint("SimpleDateFormat") SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.sss");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
report_time = simpleDateFormat.format(calendar.getTime());
Log.d("preferences", "Time stored in preferences: " + mLastUpdateTime);
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_location", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("user_latitude", lat);
editor.putString("user_longitude", lng);
editor.putString("getLatLongIf", "ok");
editor.putString("report_time_for_location_response", report_time);
editor.putString("time_when_comes_to_getLatLong", timeWhenGetInGetLatLong);
editor.apply();
}else{
timeFromElse = DateFormat.getTimeInstance().format(new Date());
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_location", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("getLatLongElse", timeFromElse);
editor.apply();
}
}
@Override
public void onDestroy() {
super.onDestroy();
timeWhenServiceDestroyed = DateFormat.getTimeInstance().format(new Date());
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_location", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("onDestroyService", timeWhenServiceDestroyed);
editor.apply();
if (googleLocationService != null) {
googleLocationService.stopLocationUpdates();
}
Log.d("onDestroyService", "onDestroy: + " + timeWhenServiceDestroyed);
}
@Override
public void onCreate() {
super.onCreate();
mLastUpdateTime = "";
Log.d("onCreateService", "onCreateService");
googleLocationService = new GoogleLocationService(context, new LocationUpdateListener() {
@Override
public void canReceiveLocationUpdates() {
}
@Override
public void cannotReceiveLocationUpdates() {
}
//update location to our servers for tracking purpose
@Override
public void updateLocation(Location location) {
if (location != null ) {
mCurrentLocation = location;
mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
Log.d("mCurrentLocation", "Location is: " + location);
getLatLong();
SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("user_location", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("TimeWhenCameToOnLocationChanged", mLastUpdateTime);
editor.apply();
Log.d("onLocationchanged", "OnLocationChangedInServiceLocation");
Log.d("Time ", "Time of lat and long from Service: " + mLastUpdateTime);
}
}
@Override
public void updateLocationName(String localityName, Location location) {
googleLocationService.stopLocationUpdates();
}
});
googleLocationService.startUpdates();
}
试试这个,希望这对你有所帮助。请记住,您将在 updateLocation 上获得更新的纬度和经度。