为啥 Android 服务中没有调用 OnlocationChanged?

Posted

技术标签:

【中文标题】为啥 Android 服务中没有调用 OnlocationChanged?【英文标题】:Why is OnlocationChanged not called in Android service?为什么 Android 服务中没有调用 OnlocationChanged? 【发布时间】:2016-01-05 04:29:52 【问题描述】:

我有一项服务会在后台线程中发送位置更新。我已将清单中的服务声明为:

<service android:name="mypackage.LocationUpdater"></service>

我从 OnStartComand 中的活动中获取数据。但是 OnLocationChanged 根本没有被调用。我无法从服务中的任何位置调用 sendLocation() 方法。

public class LocationUpdater extends Service implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener 

    LocationListener mLocationListener;
    LocationRequest mLocationRequest;
    GoogleApiClient mGoogleApiClient;
    Location mLastLocation; //this object should be used to request location updates
    String p = "test", d = "test";
    @Override
    public void onCreate() 
        buildGoogleApiClient();

    

    protected synchronized void buildGoogleApiClient() 
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    

    protected void createLocationRequest() 
        LocationRequest mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(5000);
        mLocationRequest.setFastestInterval(3000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) 
        p = intent.getStringExtra("ptok");
        d = intent.getStringExtra("dtok");
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_NOT_STICKY;
    


    @Override
    public void onConnected(Bundle connectionHint) 
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);

            mLocationRequest = LocationRequest.create();
            mLocationRequest.setInterval(5000);
            mLocationRequest.setFastestInterval(3000);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

        if (mLastLocation != null) 
            String latitude = String.valueOf(mLastLocation.getLatitude());
            String longitude = String.valueOf(mLastLocation.getLongitude());
            sendLocation(p, latitude, longitude, d);
        
    

    @Override
    public void onConnectionSuspended(int i) 

    

    @Override
    public void onLocationChanged(Location location) 
        // TODO Auto-generated method stub

            if(location != null) 
                double latitude = location.getLatitude();
                double longitude = location.getLongitude();
                Log.i("info", "Latitude :: " + latitude);
                Log.i("info", "Longitude :: " + longitude);
                //sending location details
                sendLocation(p, String.valueOf(latitude), String.valueOf(longitude), d);
            
    

我还必须添加 onConnected 也永远不会被调用。我所看到的只是 onStartComand 被调用并使用意图传递从我的活动发送的值。

【问题讨论】:

【参考方案1】:

文档没有描述这一点,但based on this SO accepted answer,我必须明确调用 mGoogleApiClient.connect();在我的 onCreate 方法中。你在 onStart(); 中调用它从活动。

【讨论】:

以上是关于为啥 Android 服务中没有调用 OnlocationChanged?的主要内容,如果未能解决你的问题,请参考以下文章

为啥当android在java中调用第二次Web服务时会创建新的会话

为啥 Android 中 Toolbar.setTitle 没有效果

为啥我会收到“服务未注册”异常,即使我没有在 Android - Java/Kotlin 中使用任何服务?

为啥在调试 android google maps 示例时没有调用我的 onLocationChanged() 方法?

在android中,为啥我的服务在按下电源按钮后没有进入睡眠状态

为啥android中都需要传递参数Context