带有gps服务的Android应用程序将阻止设备,直到重新启动

带有gps服务的Android应用程序将阻止设备,直到重新启动,android,gps,Android,Gps,关于Android上的GPS,我有一个非常奇怪的问题。我有一个应用程序正在使用com.google.android.gms.location.LocationClient,每15秒请求一次locationUpdate。它托管在后台服务中,并跟踪用户的位置。几乎所有时间都工作正常,如果手机长时间处于没有任何位置提供商(GPS、Wi-Fi、手机等)的地方(例如在地下室、车库等),就会出现问题。离开该位置并接收到新位置后,整个设备会阻塞,需要重新启动(卸下电池)才能继续工作。。你见过这种行为吗?你知道

关于Android上的GPS,我有一个非常奇怪的问题。我有一个应用程序正在使用com.google.android.gms.location.LocationClient,每15秒请求一次locationUpdate。它托管在后台服务中,并跟踪用户的位置。几乎所有时间都工作正常,如果手机长时间处于没有任何位置提供商(GPS、Wi-Fi、手机等)的地方(例如在地下室、车库等),就会出现问题。离开该位置并接收到新位置后,整个设备会阻塞,需要重新启动(卸下电池)才能继续工作。。你见过这种行为吗?你知道怎么处理吗

startService(new Intent(this, GPSService.class));
这就是服务:

public class GPSService extends Service implements LocationListener,
    ConnectionCallbacks, OnConnectionFailedListener {

private LocationClient locationclient;
private LocationRequest locationrequest;

private void InitGpsService() {
    if (locationclient != null && locationclient.isConnected()) {
        return;
    }
    int resp = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (resp == ConnectionResult.SUCCESS) {
        locationclient = new LocationClient(this, this, this);
        locationclient.connect();

        Log.d("Messangero", "Location Client Connect");

    } else {
        Toast.makeText(this, "Google Play Service Error " + resp,
                Toast.LENGTH_LONG).show();
    }
}

// Binder given to clients
private final IBinder mBinder = new LocalBinder();

public class LocalBinder extends Binder {
    GPSService getService() {
        // Return this instance of LocalService so clients can call public
        // methods
        return GPSService.this;
    }
}

public IBinder onBind(Intent arg0) {
    return mBinder;
}

public void onCreate() {
    super.onCreate();
    InitGpsService();
};

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);

};

public void onDestroy() {
    super.onDestroy();
    if (locationclient != null && locationclient.isConnected()) {
        locationclient.removeLocationUpdates(this);
        locationclient.disconnect();
    }
}

public void onConnectionFailed(ConnectionResult arg0) {
    // TODO Auto-generated method stub

}

public void onConnected(Bundle arg0) {
    // TODO Auto-generated method stub
    PreferencesUtil.LoadSettings(this);

    locationrequest = new LocationRequest();
    locationrequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationrequest.setInterval(PreferencesUtil.GPSSyncPeriod);
    locationclient.requestLocationUpdates(locationrequest, this);
}

public void onDisconnected() {
    // TODO Auto-generated method stub
    if (locationclient != null && locationclient.isConnected())
        locationclient.removeLocationUpdates(this);

}

public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    Thread thr = new Thread(new LocationUpdateThread(GPSService.this,
            location));
    thr.start();
}
}

服务在主线程中运行!!!!这就是为什么它是冻结用户界面

警告:服务在其宿主进程的主线程中运行。服务不会创建自己的线程,也不会在单独的进程中运行(除非您另有指定)。这意味着,如果您的服务要执行任何CPU密集型工作或阻塞操作(如MP3播放或联网),您应该在服务中创建一个新线程来执行该工作。通过使用单独的线程,您将降低应用程序不响应(ANR)错误的风险,并且应用程序的主线程可以保持专用于用户与活动的交互


例如,您应该通过IntentService来完成。或者在该服务上添加处理程序/线程。

似乎很奇怪。。。当你说“后台服务”时,你到底使用了什么?问题是崩溃的不是应用程序。。。重新启动前,整个手机都会阻塞。这个问题无法回答为什么整个手机会阻塞。我会尝试一下,看看是否会有什么变化