2017-05-17 25 views
1

バックグラウンドサービスを実行して、継続的な位置情報の更新を試みています。コードonConnected(バンドルb)がデバッグされ、位置更新要求が呼び出されます。ただし、onLocationChanged(Location location)は呼び出されません。以下は私のコードです:onLocationChangedが呼び出されない、GoogleApiClient

public class LocationUpdateService extends Service implements 
     GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { 


    private GoogleApiClient googleApiClient; 
    private LocationRequest mLocationRequest; 
    Location mCurrentLocation; 

    @Override 
    public void onLocationChanged(Location location) { 
     mCurrentLocation = location; 
     double lat = mCurrentLocation.getLatitude(); 
     double lng = mCurrentLocation.getLongitude(); 
    } 

    //GoogleApiClient 
    @Override 
    public void onConnectionFailed(ConnectionResult bundle) { 

    } 

    @Override 
    public void onConnected(Bundle bundle) { 
     Log.i("onConnected", "GoogleApiClient"); 

     Toast.makeText(this, "Location service connected", Toast.LENGTH_SHORT).show(); 
     createLocationRequest(); 
     startLocationUpdate(); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 

    //Service 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
     buildGoogleApiClient(); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     return START_STICKY; // run until explicitly stopped. 
    } 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    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; 
     } 
     LocationServices.FusedLocationApi.requestLocationUpdates(
       googleApiClient, mLocationRequest, this); 
     mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 
    } 

    void buildGoogleApiClient() { 
     googleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 
     googleApiClient.connect(); 
    } 

    void createLocationRequest() { 
     mLocationRequest = new LocationRequest().create() 
       .setInterval(5000) 
       .setFastestInterval(5000) 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    } 
} 

私はandroid docsを踏襲しているが、私は、間違いを犯していますどこ私は理解することはできません。私は実際のデバイスでテストしており、アプリケーションにはロケーションのパーミッションがあります。 menifestで

サービス:

<service 
      android:name=".locations.LocationUpdateService" 
      android:enabled="true" 
      android:exported="false"></service> 

活動中のサービスの呼び出し:

startService(new Intent(BaseActivity.this, LocationUpdateService.class)); 
+0

まず、マニフェストファイルでこのサービスについて言及してから、このサービスをあなたのアクティビティまたはフラグメントにバインドする必要があります。 – Bhavnik

+0

@Bhavnik私はすでにmenifestでサービスを言及しており、サービスもアクティビティから開始しています。デバッグ中に** OnConnected()**メソッドを使用します。 ** onLocationChanged **を呼び出すことはありません** –

答えて

0

何あなたのAndroid携帯電話のSDKで、どのようにマニフェストファイルのパーミッションを求めていますか? 電話SDKが23以上の場合、アクセス許可を求める方法が異なります。

0

これは単なるデバッグの問題でした。私のデバイスの場所が見つかりました。アクセスは事前設定でオフになっていました。

関連する問題