【问题标题】:Retrieve location from Service从服务中检索位置
【发布时间】:2014-04-02 15:55:43
【问题描述】:

我正在尝试做一个服务,监听用户的位置。

代码如下:

public class ServiceBeezer extends Service implements
    OnConnectionFailedListener, ConnectionCallbacks {

    private LocationRequest mLocationRequest;
    private LocationClient mLocationClient;

    public ServiceBeezer() {
    }

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

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

        // 1. init locationrequest
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setInterval(1000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_NO_POWER);
        mLocationRequest.setFastestInterval(1000);

        // 2. mlocationclient
        mLocationClient = new LocationClient(this, this, this);

        return START_STICKY;
    }

    @Override
    public void onConnected(Bundle arg0) {
        Toast.makeText(this,
                getClass().getSimpleName() + "onConnected: " + arg0,
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onDisconnected() {
        Toast.makeText(this, getClass().getSimpleName() + "onDisconnected: ",
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {
        Toast.makeText(this,
                getClass().getSimpleName() + "onConnectionFailed: " + arg0,
                Toast.LENGTH_LONG).show();
    }

}

但是 onConnected()onDisconnected()onConnectionFailed() 永远不会被调用。

我做错了什么?

我在 Activity 上有其他 LocationRequest 和 LocationClient。

可能是这个问题吗?

【问题讨论】:

  • 绑定活动代码是什么样的?
  • startService(new Intent(this, ServiceBeezer.class));
  • 在继续之前,StartService 调用应该返回一个 ComponentName 对象。只需检查以确保它不为空,否则可能是清单问题
  • startService 返回一个对象,名称为“ServiceBeezer”
  • 我认为你缺少你的活页夹,通常你创建一个扩展活页夹的本地类,这可以传回对这个服务器的引用,目前你从你的 onBind 函数返回 null,看看这个教程@ 987654321@它对我有用:)

标签: java android service location location-client


【解决方案1】:

如果我没记错的话,你没有在上面粘贴的代码中的任何地方调用 mLocationClient.connect()。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 1970-01-01
    相关资源
    最近更新 更多