【发布时间】:2016-12-21 00:10:02
【问题描述】:
我正在使用以下代码请求位置更新。在单独的处理程序线程中请求位置更新,但它仍然会影响主线程。任何人都可以提出一个解决方案。
private LocationListener mLocationListener = new LocationListener() {
public void onLocationChanged(Location location) {
mLongitude = location.getLongitude();
mLatitude = location.getLatitude();
mBearing = location.getBearing();
}
}
mLocationManager = (LocationManager)
this.getSystemService(Context.LOCATION_SERVICE);
HandlerThread handlerThread = new HandlerThread("HandlerThread");
handlerThread.start();
Looper looper = handlerThread.getLooper();
mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
UPDATE_LOCATION_INTERVAL,
UPDATE_LOCATION_MINIMUM_DISTANCE,
mLocationListener, looper);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
UPDATE_LOCATION_INTERVAL,
UPDATE_LOCATION_MINIMUM_DISTANCE,
mLocationListener, looper);
【问题讨论】:
-
你怎么知道它会影响主线程?
-
我正在使用主线程通过网络接收视频,它受到影响。当我禁用位置更新时它工作正常,但位置更新开始失败。
-
“我正在使用主线程通过网络接收视频”——这本身就很糟糕。不要在主应用程序线程上执行网络 I/O。您应该使用
NetworkOnMainThreadException崩溃。 -
更准确地说,有一个监听器,在主线程中监听视频块。套接字连接、接收在不同的线程中处理,而不是在主线程中。
标签: android multithreading android-location android-handler