2013-12-17 64 views
-1

從Android官方網站我已下載位置服務示例&確實在真實設備上運行。每次onLocationChanged方法都返回經緯度但不準確的GPS位置。如何從新的android位置API實現準確的GPS位置。Gps Android中的位置更新不起作用

這裏是我的服務代碼

package com.example.locationservice; 

import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.util.Log; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.GooglePlayServicesClient; 
import com.google.android.gms.common.GooglePlayServicesUtil; 
import com.google.android.gms.location.LocationClient; 
import com.google.android.gms.location.LocationRequest; 

public class MDFYLocationUpdate extends Service implements 
LocationListener, 
GooglePlayServicesClient.ConnectionCallbacks, 
    GooglePlayServicesClient.OnConnectionFailedListener, 
    com.google.android.gms.location.LocationListener { 

LocationClient mLocationClient; 
    private LocationRequest mLocationRequest; 
    public static Context c; 
    final String BROADCAST_ACTION ="current_location"; 
    public static final String TAG="Location"; 
// Milliseconds per second 
    private static final int MILLISECONDS_PER_SECOND = 1000; 
    // Update frequency in seconds 
    public static final int UPDATE_INTERVAL_IN_SECONDS = 5; 
    // Update frequency in milliseconds 
    private static final long UPDATE_INTERVAL = 10000; 
      //MILLISECONDS_PER_SECOND * UPDATE_INTERVAL_IN_SECONDS; 
    // The fastest update frequency, in seconds 
    private static final int FASTEST_INTERVAL_IN_SECONDS = 1; 
    // A fast frequency ceiling in milliseconds 
    private static final long FASTEST_INTERVAL = 
      MILLISECONDS_PER_SECOND * FASTEST_INTERVAL_IN_SECONDS; 

    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     return null; 
    } 
    @Override 
    public void onCreate() { 
     // TODO Auto-generated method stub 
     super.onCreate(); 
     mLocationRequest =LocationRequest.create(); 

       mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); 
     // Set the update interval to 5 seconds 
     mLocationRequest.setInterval(UPDATE_INTERVAL); 
     // Set the fastest update interval to 1 second 
     mLocationRequest.setFastestInterval(FASTEST_INTERVAL); 
     servicesConnected(); 
     mLocationClient =new LocationClient(this, this, this); 
    } 
    @Override 
    public void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
    } 


    @Override 
    public void onConnected(Bundle connectionHint) { 
     // TODO Auto-generated method stub 

     Log.d(TAG, "onconnected"); 
     mLocationClient.requestLocationUpdates(mLocationRequest, this); 
    } 
    @Override 
    public void onDisconnected() { 
     // TODO Auto-generated method stub 
     Log.d(TAG, "onsisconnect"); 
     // Destroy the current location client 
     mLocationClient = null; 
    } 
    @Override 
    public void onConnectionFailed(ConnectionResult result) { 
     // TODO Auto-generated method stub 

    } 

    private boolean servicesConnected() { 
     // Check that Google Play services is available 
     int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
     // If Google Play services is available 
     if (ConnectionResult.SUCCESS == resultCode) { 

     return true; 
     } else { 

     return false; 
     } 
     } 

    @Override 
    public void onLocationChanged(Location location) { 
     // TODO Auto-generated method stub 
     Log.d(TAG, "onLocationChanged"); 
     Log.d(TAG, "Lat = " + location.getLatitude() + " Lng = " + location.getLongitude()); 
     Logger.writeLog("Lat = " + location.getLatitude() + " Lng = " + location.getLongitude() + " == "+BasicDeviceInformation.getCurrentDate()); 

    } 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     // TODO Auto-generated method stub 
     Log.d(TAG, "onStartCommand"); 
     mLocationClient.connect(); 
     return super.onStartCommand(intent, flags, startId); 
    } 
    @Override 
    public void onProviderDisabled(String provider) { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void onProviderEnabled(String provider) { 
     // TODO Auto-generated method stub 

    } 
    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
     // TODO Auto-generated method stub 

    } 
} 
+1

你可以張貼在界定使用提供商的代碼? – NigelK

回答

0

不知道用戶是否會回到SO或沒有,所以張貼他人誰遇到這個問題代表了他的答案。

PS: - 這個答案是由用戶自己提出的,但是在錯誤的地方,所以發表了他的名字。

UPDATE

我發現這個問題的解決方案....我錯過了在清單中的元數據標籤 。

將下面的代碼放在應用程序標籤中的清單中。

<meta-data android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 

放置這一個後,我得到正確的位置。

問題已經結束