屏幕关闭后无法接收LocationClient的位置更新

use*_*028 5 android android-service android-maps-v2

我正在尝试编写一个在后台运行的简单Android服务,并从LocationClient(Google Map API Android V2)接收位置更新.问题是,当屏幕关闭时,我的服务不再接收位置更新.我试图检查服务是否处于活动状态,即使关闭屏幕也是如此(它有一个TimerTask调度日志).当屏幕打开时,我可以接收位置更新,但是当屏幕关闭时,我只看到TimerTask的日志,我没有收到任何位置更新.唤醒屏幕再次打开位置更新.怎么解决这个问题?

这是我的简单服务:

public class LocationService extends Service implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationListener{

private static final String TAG = LocationService.class.getSimpleName();

private LocationClient mLocationClient;

private Timer timer;
private static final LocationRequest REQUEST = LocationRequest.create()
          .setInterval(5*1000)      // 5 seconds
          .setFastestInterval(3*1000) // 3 seconds
          .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);


@Override
public void onCreate() {
    Log.d(TAG, "Creating..");
    mLocationClient = new LocationClient(this, this, this);
    timer = new Timer();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(TAG, "Starting..");
    if(!mLocationClient.isConnected() || !mLocationClient.isConnecting()) {
        Log.d(TAG, "Connecting location client..");
        mLocationClient.connect();
    }
    timer.scheduleAtFixedRate(new TimerTask(){
        @Override
        public void run() {
            Log.d(TAG, "TimerTask executing");
        }}, 0, 3*1000);
    return START_STICKY;
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    Log.d(TAG, "Connection failed..");
    stopSelf();
}

@Override
public void onConnected(Bundle bundle) {
    System.out.println("Connected ...");
    mLocationClient.requestLocationUpdates(REQUEST, this);
}

@Override
public void onDisconnected() {
    Log.d(TAG, "Disconnected..");
    stopSelf();
}

@Override
public IBinder onBind(Intent arg0) {
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public void onLocationChanged(Location location) {
    Log.d(TAG, "Received location update");
}

@Override
public void onDestroy() {
    Log.d(TAG, "Destroying..");
    timer.cancel();
    mLocationClient.removeLocationUpdates(this);
}
}
Run Code Online (Sandbox Code Playgroud)

小智 0

我不确定你的问题出在哪里。我已经用您的代码创建了一个项目,它似乎在这里按预期工作。即使屏幕关闭时我也收到更新。无论如何,这是我采取的步骤,也许您的问题不在于服务,而在于您如何启动它,或者其他地方:

  1. 我创建了一个默认的 android 项目,其中包含一个主要活动。
  2. 我将您的代码包含在名为“LocationService”的单独文件中
  3. 我将 google play 服务库添加到项目中。
  4. 我在清单中添加了以下权限:

    < 使用权限 android:name="android.permission.ACCESS_FINE_LOCATION" /> < 使用权限 android:name="android.permission.ACCESS_COARSE_LOCATION" />

在应用程序标签中我声明了该服务:

   <service
        android:name=".LocationService"
        android:process=":Location_Service_Test" />
Run Code Online (Sandbox Code Playgroud)

最后我将以下内容添加到主要活动的 onCreate 中:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Start the service
    Intent service = new Intent(this, LocationService.class);
    startService(service);
} 
Run Code Online (Sandbox Code Playgroud)

如果这不能解决您的问题:请包含其余的代码。