我正在使用位置更新应用程序,它将定期将用户位置更新到服务器。
我已经使用融合位置提供程序来获得位置更新。我已经参考了以下链接Android Location Update。
关于我下面的clarifications,中的位置更新
( 1)我使用待定的意图请求定位。在定位请求中,我给出了5分钟的时间间隔。我每隔5分钟就能成功地得到位置信息。但我的问题是,“Android如何有效地使用GPS定位--全球定位系统( GPS )不需要在整个5分钟内启动”。大约4分50秒,它开始使用全球定位系统来获取用户的位置。我只想知道融合定位提供者如何有效地利用GPS。 2)此外,我还想知道使用可用提供者获取用户位置所需的时间。android使用融合定位提供者获取用户位置所花费的时间大约是多少。 3)融合定位提供者仅在给定间隔的最近时间使用GPS来获取用户位置。剩余的时间GPS是不可用的。是否有更好的解决方案,关闭GPs,或有效地使用GPS,以节省移动电池的能量。 4)给出时间间隔为1 min。有些时候不是每一分钟更新一次位置。例如,成功的第一分钟我得到用户的位置。然后在第二分钟不知道用户的位置。然后第三分钟就好了。我有全球定位系统和移动网络可用和WiFi连接。 ( 5)我们能给出的最大时间间隔是多少。在文档中找不到任何最长时限。我们可以给出最小时间间隔为零。但这是不推荐的。 6)此外,我还想知道在使用此功能时对操作系统的最低支持程度。我在下面的链接Does Google Activity Recognition work on older versions of Android?中提到,Google服务中的所有内容都应该回到APILevel8(Android2.3)。
请帮帮我。我希望这可以帮助其他开发者谁也是谁正在进行Android位置更新。
提前谢谢。
发布于 2015-03-12 04:55:35
I have used the fused location provider API's in our project and it's was really helpful to improve battery usage.Earlier We were using Android's Location framework APi's .
Please read below article which I have prepared ,you can refer the information to implement it in your project.
In simple words, it’s the best way to get location data in Android platform as of now.
GooglePlay Services provide many location APi’s to get the location data(e.g. User’s current location or you can say device’s last known location).
The Fused Location Provider is one of the location APIs in Google Play services.
Prerequisite is that:
1- Google Play services sdk is used as library project(and also Google PlayService is properly installed in your device)
Download and install the Google Play services component from the SDK Manager and add the library to your project.
Import GooglePlayServices lib from android google-play-services_lib in your development project as Library project.
2- You should have an actual device as this APi won’t work in Emulator.
The Fused Location Provider intelligently manages the underlying location technology (GPS/Wi-Fi/Network provider’s connection) and gives us the best location according to our needs.
Why to use
=============
We could choose one of the location providers (network or GPS) and request location updates or set up proximity alert. But there were two main issues with this approach:
1. In case we need to define precise location, we had to switch between network and GPS location providers (as GPS doesn’t work indoors).
2. Proximity alerts were used to notify a user about proximity to a location, and this took its toll on the battery life.
Benefits
==========
1. Simple APIs: Lets us specify high-level needs like “high accuracy” or “low power”, instead of having to worry about location providers.
2. Battery efficient: Minimizes out app’s use of power. Based on all incoming location requests and available sensors, fused location provider chooses the most efficient way to meet those needs.
Steps to use this Api:
=====================
1- Declare the location related permission in the manifest file.
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION"/>
Or
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
2. Implement related interfaces and callbacks.
Before we request location updates, we must first implement the interfaces that Location Services uses to communicate connection status to our app:
2.1 com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks: Specifies methods that Location Services calls when a location client is connected or disconnected.
2.2
com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener
3 Connect to Google Play Services
Connecting LocationClient to Google api.To do this , create a LocationClient object (it’s actually instance of GoogleApiClient object) as below:
mLocationClient = new GoogleApiClient.Builder(getApplicationContext())
.addApi(LocationServices.API).addConnectionCallbacks(this)
.addOnConnectionFailedListener(this).build();
and then call connect() :
mLocationClient.connect();
4- Create an instance of FusedLocationProviderApi by using LocationServices class as below:
private FusedLocationProviderApi fusedLocationProviderApi = LocationServices.FusedLocationApi;
5- Retrieve the current location
Inside onConnected(Bundle bundle){
Location currentLocation = fusedLocationProviderApi .getLastLocation(mLocationClient);
if (mCurrentLocation != null) {
Log.d(TAG, "last location =" + mCurrentLocation.getLatitude()
+ " - " + mCurrentLocation.getLongitude());
}
}
6- Receive periodic location updates
Create and setup LocationRequest object:
mLocationRequest = new LocationRequest();
private void setLocationParameter() {
// Set the update interval
mLocationRequest.setInterval(Constants.SECONDS_TO_UP);
// Use high accuracy
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
// Set the interval ceiling to one minute
mLocationRequest.setFastestInterval(Constants.SECONDS_TO_UP);
// Set the distance to 10 meters.
mLocationRequest.setSmallestDisplacement(Constants.METERS_TO_UP);
}
6- Request for periodic Location updates: To get periodic location updates from Location Services, we send a request using a location client.
LocationListener listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Utils.locationUpdates = Utils.locationUpdates + 1;
if (Utils.locationUpdates == 1) {
mLocationRequest
.setPriority(LocationRequest.PRIORITY_LOW_POWER);
LocationServices.FusedLocationApi.requestLocationUpdates(
mLocationClient, mLocationRequest, listener);
}
}
}
};https://stackoverflow.com/questions/27033715
复制相似问题