首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未从服务调用Awareness.SnapshotApi.getLocation中的onResult()

未从服务调用Awareness.SnapshotApi.getLocation中的onResult()
EN

Stack Overflow用户
提问于 2016-10-20 11:25:01
回答 1查看 247关注 0票数 5

我正在尝试从后台Service获取位置。问题是Awareness.SnapshotApi.getLocation(mGoogleApiClient).setResultCallback(new ResultCallback<LocationResult>()中的onResult()函数没有被调用。这里会有什么问题呢?

函数onConnected()onConnectionSuspended()也没有被调用,但是当我打印输出时,返回mGoogleApiClient.isConnected() = true

所以我试图理解为什么onConnected()也没有被调用。

代码语言:javascript
复制
public class BlockingService extends Service implements GoogleApiClient.ConnectionCallbacks {
    private GoogleApiClient mGoogleApiClient;

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

        initAwarenessAPI();
        amIClosetoLocation(<LatLng I get from my database>);

        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {

        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
        super.onDestroy();
    }

    private void initAwarenessAPI() {
        Context context = getApplicationContext();
        mGoogleApiClient = new GoogleApiClient.Builder(context)
                .addApi(Awareness.API)
                .build();
        mGoogleApiClient.connect();
    }

    private Boolean amIClosetoLocation(final LatLng savedLoc) {
        final String TAG = "Awareness";
        mLog.printToLog(className + Constants.funcStart + MethodName.methodName() + ": Entered Function");
        amIClosetoLocation = false;

        if (ContextCompat.checkSelfPermission(
                getApplicationContext(),
                android.Manifest.permission.ACCESS_FINE_LOCATION) !=
                PackageManager.PERMISSION_GRANTED) {
            mLog.printToLog(className + MethodName.methodName() + ": ***DONT HAVE LOCATION PERMISSION***");
        } else {
            mLog.printToLog(className + MethodName.methodName()
                    + ": Permission Exists -- so now going to get location");

            Awareness.SnapshotApi.getLocation(mGoogleApiClient)
                    .setResultCallback(new ResultCallback<LocationResult>() {
                        @Override
                        public void onResult(@NonNull LocationResult locationResult) {
                            //THIS FUNCTION IS NOT GETTING CALLED
                            if (!locationResult.getStatus().isSuccess()) {
                                Log.e(TAG, "Could not get location.");

                                mLog.printToLog(className + MethodName.methodName() + ": Could NOT get location");

                                return false;
                            } else {
                                mLog.printToLog(className + MethodName.methodName()
                                        + ": Success: Able to send location");
                            }
                            Location currLocation = locationResult.getLocation();

                            mLog.printToLog(className + MethodName.methodName()
                                    + ": Success, Received location = " + currLocation.toString());

                            mLog.printToLog(className + Constants.funcStart + MethodName.methodName() +
                                    ": Got Location = Lat: " +
                                    currLocation.getLatitude() +
                                    ", Lon: " + currLocation.getLongitude());
                        }
                    });
        }


        mLog.printToLog(className + Constants.funcEnd + MethodName.methodName()
                + ": Exiting Function, returning amIClosetoLocation = " + amIClosetoLocation);


        return amIClosetoLocation;
    }


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

    mLog.printToLog(className + Constants.funcStart + MethodName.methodName() + ": Entered Function");


@Override
public void onConnectionSuspended(int i) {

}
}
EN

回答 1

Stack Overflow用户

发布于 2016-11-18 20:17:27

new GoogleApiClient.Builder(context)的第二个参数是回调监听器,您没有注意到。

我的意思是:new GoogleApiClient.Builder(context, this)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40144912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档