我想编写一个基于位置的客户端/服务器应用程序 - Java / Android。 我是这个领域的新手。 应用程序在使用时会使用用户位置为其提供一些信息。 我应该运行"客户端"与服务器交互的对象作为"服务"从主线程 - Android应用程序的UI线程? (我想也许它可能对于与服务器同步信息很有用,比如用GPS位置更新服务器并将与该位置相关的信息保存在设备的缓存中,可能更新应用程序版本,也许可能有用的东西,我现在不能想到他们)
首先,这是使用服务类的正确方法吗? 基本上,信息只有在他特别询问时才能提供给客户,但我想也许这样,我可以改善客户体验(当然在服务器能力范围内处理来自所有用户的信息)< / p>
第二,更新服务器的位置是一项消耗大量电池的操作?超过&#34;常规应用&#34;在设备上运行,如电子邮件,脸书,在非导航模式下等待?
答案 0 :(得分:1)
您可以使用此位置提供程序类来帮助您:
public class LocationProvider implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
public abstract interface LocationCallback {
public void handleNewLocation(Location location);
}
public static final String TAG = LocationProvider.class.getSimpleName();
/*
* Define a request code to send to Google Play services
* This code is returned in Activity.onActivityResult
*/
private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private LocationCallback mLocationCallback;
private Context mContext;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
public LocationProvider(Context context, LocationCallback callback) {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mLocationCallback = callback;
// Create the LocationRequest object
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000)
.setFastestInterval(1 * 1000);
mContext = context;
}
public void connect() {
mGoogleApiClient.connect();
}
public void disconnect() {
if (mGoogleApiClient.isConnected()) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
mGoogleApiClient.disconnect();
}
}
public void refresh() {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Location services connected.");
Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (location == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
else {
mLocationCallback.handleNewLocation(location);
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
/*
* Google Play services can resolve some errors it detects.
* If the error has a resolution, try sending an Intent to
* start a Google Play services activity that can resolve
* error.
*/
if (connectionResult.hasResolution() && mContext instanceof Activity) {
try {
Activity activity = (Activity)mContext;
// Start an Activity that tries to resolve the error
connectionResult.startResolutionForResult(activity, CONNECTION_FAILURE_RESOLUTION_REQUEST);
/*
* Thrown if Google Play services canceled the original
* PendingIntent
*/
} catch (IntentSender.SendIntentException e) {
// Log the error
e.printStackTrace();
}
} else {
/*
* If no resolution is available, display a dialog to the
* user with the error.
*/
Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode());
}
}
@Override
public void onLocationChanged(Location location) {
mLocationCallback.handleNewLocation(location);
}
}
然后使用.refresh方法获取可能存储的位置更新,以及您希望的位置。
至于电池问题,Google Play服务会定期使用电池来请求定位,但它不应该 对用户或您来说是显而易见的,因为它与其他电池相比较小使用手机的机制,如屏幕。
答案 1 :(得分:1)
我使用过这种基于位置的应用程序。获取位置的最佳方法是每次通过实现LocationListener接口的onLocationChanged()回调更改位置时获取它。并且当用户想要使用LocationManager类时,也可以获取位置。这样您也可以获得更新的位置并节省一些电池。我有一些应用程序的实现,您可以将其用作参考:https://github.com/kodered/TrackMe-Ver-2.0 希望这可以帮助 !