0

こんにちは、現在、GoogleApiClient を使用して Android デバイスの緯度と高度の値を取得しようとしています。ここのガイドに従っています:

GoogleApiClient: 最後の既知のロケーション ガイド

また、アクティビティではなくサービスからこれを実行しようとしています。参照用のコードのスニペットを次に示します。

public class AppService extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

private GoogleApiClient googleApiClient;

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

@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
    if (googleApiClient == null) {
        googleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }
    googleApiClient.connect();
    try {
        Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
        Log.d("location", String.valueOf(lastLocation.getLatitude()));
        Log.d("location", String.valueOf(lastLocation.getLongitude()));
    } catch (SecurityException e) {
        Log.e("Location error", "Location object is null.");
    }

    googleApiClient.disconnect();

    //Stop service once it finishes its task
    stopSelf();
    return Service.START_STICKY;
}

@Override
public void onConnected(@Nullable Bundle bundle) {

}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}
}

緯度と高度を取得するために Location 変数にアクセスしようとするたびに、NullPointerException が発生します。

また、念のため AndroidManifest ファイルを含めたいと思います。私の知る限り、必要な権限があります。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="app"
      android:versionCode="1"
      android:versionName="1.0" >
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application android:label="@string/app_name" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </activity>
    <receiver
            android:name=".AutoStart"
            android:enabled="true" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>
    <service android:name=".AppService"/>
</application>
</manifest>
4

1 に答える 1

1

ドキュメントによると、への呼び出しgoogleApiClient.connect()は非同期です。

公共抽象無効接続 ()

クライアントを Google Play サービスに接続します。このメソッドはすぐに戻り、バックグラウンドでサービスに接続します。接続が成功すると、onConnected(Bundle) が呼び出され、キューに入れられたアイテムが実行されます。失敗すると、onConnectionFailed(ConnectionResult) が呼び出されます。

クライアントがすでに接続されているか、接続中の場合、このメソッドは何もしません。

このため、onConnectedコールバックが実行されるのを待って、そこに位置取得コードを配置する必要があります。

@Override
public int onStartCommand(final Intent intent, int flags, int startId) {
  if (googleApiClient == null) {
     googleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
  }
  googleApiClient.connect();
  return Service.START_STICKY;
}

@Override
public void onConnected(@Nullable Bundle bundle) {
  try {
    Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    Log.d("location", String.valueOf(lastLocation.getLatitude()));
    Log.d("location", String.valueOf(lastLocation.getLongitude()));
  } catch (SecurityException e) {
    Log.e("Location error", "Location object is null.");
  }

  googleApiClient.disconnect();
  stopSelf();
}
于 2016-09-26T20:24:06.690 に答える