首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >onLocationChanged从未打过电话,GoogleApiClient

onLocationChanged从未打过电话,GoogleApiClient
EN

Stack Overflow用户
提问于 2017-05-17 02:50:31
回答 2查看 464关注 0票数 2

我试图通过运行后台服务来获得连续的位置更新。在调试代码时,调用了onConnected(Bundle b),并调用了位置更新请求。但是onLocationChanged( location )从未被调用过。下面是我的代码:

代码语言:javascript
运行
AI代码解释
复制
    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文档的时候,我不明白我在哪里犯了错误。我正在测试真实的设备和应用程序的位置权限。

最优惠服务:

代码语言:javascript
运行
AI代码解释
复制
<service
            android:name=".locations.LocationUpdateService"
            android:enabled="true"
            android:exported="false"></service>

活动中的服务召唤:

代码语言:javascript
运行
AI代码解释
复制
startService(new Intent(BaseActivity.this, LocationUpdateService.class));
EN

回答 2

Stack Overflow用户

发布于 2017-05-17 07:00:24

您的android是什么?您是如何要求清单文件的权限的?如果您的phone SDK是23或更高,则请求权限的方式是不同的。

票数 0
EN

Stack Overflow用户

发布于 2017-05-19 01:55:33

这只是一个愚蠢的调试问题。我发现我的设备位置访问在预先设置是关闭的。

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

https://stackoverflow.com/questions/44022751

复制
相关文章

相似问题

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