我需要每1分鐘更新一次融合位置和每個5米位移(我知道它的不好的做法,但爲了測試和在日誌中顯示),我開始按鈕按鈕服務儘快按鈕按下我在logcat中的位置,但只有一次。根據服務等級onLocationChanged應該每隔1分鐘調用一次,但它永遠不會再打電話,即使我的GPS幾乎每分鐘打開一次,然後在關閉之後關閉,但Logcat中仍然沒有位置更新。我需要服務類保留而不會影響用戶界面或主線程Android服務沒有更新位置
這裏更新位置是logcat的表示只有一次位置更新
09-16 03:13:37.125 10419-10419/com.example.com.pro_working1 D/===Location Service===: Service onConnected Calling
09-16 03:13:37.125 10419-10419/com.example.com.pro_working1 D/===Location Service===: Start Location Update is Calling
09-16 03:13:38.315 10419-10419/com.example.com.pro_working1 D/===Location Service===: Service OnLocationChanged Calling
09-16 03:13:38.315 10419-10419/com.example.com.pro_working1 D/===Location Service===: Here is Updated Locations: Latitude 28.5XXXXX Longitude 77.2XXXXX
09-16 03:13:38.315 10419-10419/com.example.com.pro_working1 D/===Location Service===: Sending Location Starting Accuracy is: 37.5
這裏是我的服務類
public class Location_Service_background extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
public static final String TAG = "===Location Service===";
GoogleApiClient apiClient;
Location mCurrentLocation;
LocationRequest locationRequest;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
GoogleApiSetup();
RequestLocationUpdates();
Log.d(TAG,"OnStartCommand Is Calling ");
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
if (apiClient.isConnected()){
Log.d(TAG,"Service On Destroy Calling and apiClient is Connected");
StopLocationUpdates();
apiClient.disconnect();
}
}
@Override
public void onLowMemory() {
super.onLowMemory();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.d(TAG,"Service onConnected Calling");
if (mCurrentLocation != null) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(apiClient);
}
else {
Log.d(TAG,"Start Location Update is Calling");
StartLocationUpdate();
}
}
@Override
public void onConnectionSuspended(int i) {
Log.d(TAG,"Service OnConnectionSuspended Calling");
apiClient.connect();
}
@Override
public void onLocationChanged(Location location) {
Log.d(TAG,"Service OnLocationChanged Calling");
mCurrentLocation=location;
Log.d(TAG,"Here is Updated Locations: Latitude "+mCurrentLocation.getLatitude()+"Longitude "+mCurrentLocation.getLongitude());
String Latitude= String.valueOf(mCurrentLocation.getLatitude());
String Longitude=String.valueOf(mCurrentLocation.getLongitude());
String Accuracy=String.valueOf(mCurrentLocation.getAccuracy());
Log.d(TAG,"Sending Location Starting"+" "+" Accuracy is: "+mCurrentLocation.getAccuracy());
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.d(TAG,"Connection Failed Called "+connectionResult);
}
private synchronized void GoogleApiSetup() {
apiClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
apiClient.connect();
}
private void RequestLocationUpdates() {
locationRequest = new LocationRequest();
//1 Min = 60 seconds AND 1000 milliseconds in 1 second
locationRequest.setInterval(60 * 1000);//5 Min 300 x 1000=300000
locationRequest.setFastestInterval(60 * 1000);//2 Min
locationRequest.setSmallestDisplacement(5);
locationRequest.setMaxWaitTime(60*1000);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
private void StopLocationUpdates() {
LocationServices.FusedLocationApi.removeLocationUpdates(apiClient, this);
}
private void StartLocationUpdate() {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
if (apiClient.isConnected()) {
LocationServices.FusedLocationApi.requestLocationUpdates(apiClient, locationRequest, this);
}
else {
Log.d(TAG,"GoogleApiClient is not connected yet.Please Try Again");
apiClient.connect();
}
}
}
嘗試不使用setsmallestdplacementment,看看會發生什麼 –
它顯示調用'onLocationUpdate'沒有'setsmallestdisplacement'但我如何使用常規更新與setsmallestdisplacement。感謝您的幫助 – androidXP