1

あなたの貴重な助けが必要です;)

デバイスの位置を定期的に修正するバックグラウンドで実行されるAndroid サービスがあります (位置ポーラー)。Google Play サービスを利用する新しい Android Location APIでコードを更新したとき。サービスを見てみましょう:

public class NewLocationPollerService extends Service implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener
{
    private LocationRequest mLocationRequest    = null;
    private LocationClient  mLocationClient     = null;

    ...
    private class PollerThread extends WakefulThread
    {
        protected void onPreExecute()
        {
            if ( GooglePlayServicesUtility.areServicesConnected( NewLocationPollerService.this ) )
                mLocationClient.requestLocationUpdates(mLocationRequest, intent);
        }

        protected void onPostExecute()
        {
            if ( GooglePlayServicesUtility.areServicesConnected( NewLocationPollerService.this ) )
                mLocationClient.removeLocationUpdates(intent);

            super.onPostExecute();
        }
    }
    ...
}

メソッド「areServicesConnected()」は次のとおりです。

public class GooglePlayServicesUtility
{
    private static final String TAG = GooglePlayServicesUtility.class.getSimpleName();

    public static boolean areServicesConnected(Context context)
    {
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
        if (ConnectionResult.SUCCESS == resultCode)
            return true;
        else
            return false;
    }
    ...
}

時々サービスがクラッシュし、ログは次のようになります:

java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
at com.google.android.gms.internal.k.B(Unknown Source)
at com.google.android.gms.internal.bh.a(Unknown Source)
at com.google.android.gms.internal.bh$c.B(Unknown Source)
at com.google.android.gms.internal.bg.requestLocationUpdates(Unknown Source)
at com.google.android.gms.internal.bh.requestLocationUpdates(Unknown Source)
at com.google.android.gms.location.LocationClient.requestLocationUpdates(Unknown Source)
at     me.app.location.NewLocationPollerService$PollerThread.onPreExecute(NewLocationPollerService.java:210    )
at me.app.thread.WakefulThread.onLooperPrepared(WakefulThread.java:79)
at android.os.HandlerThread.run(HandlerThread.java:59)
at me.app.thread.WakefulThread.run(WakefulThread.java:94)

どう思いますか?同様の投稿を読みましたが、同じものではありません。メソッド areServicesConnected() が true を返した場合でも、後で何らかの理由でサービスが切断されることがあるようです。

ありがとう、すべての助けが本当に感謝されます!

ハッピーコーディング;)

4

1 に答える 1

5

isGooglePlayServicesAvailable接続されていることを通知するのではなく、サービスの可用性を確認するだけです。

logcatにあることを正確に行う必要があります。電話LocationClient.connect()すると、コードが実装に到達すると接続されますが、その実装はConnectionCallbacks.onConnected質問には表示されません。

于 2013-10-01T20:41:25.213 に答える