在我的应用程序中,我有一个后台服务器和内部服务器类,我有计时器和内部计时器,我有一个 locationManager 来查找位置:
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 5000, 10,
locationListener);
但在这里我得到了这个错误:
java.lang.runtimeexception can't create handler inside thread that has not called looper.prepare();
我的问题是如何在计时器中使用 locationManager?
最佳答案
当您使用 runOnUiThread 时,请注意与 Activity 不同的后台服务,这在 Activity 中是可能的,请小心,或者如果您在 Service 类中使用线程,您可以这样做: 在服务类里面写这个:
static Activity ac;
public static void setActivity(Activity a) {
ac = a;
}
在你的 Activity 课上这样做:
MyService.setActivity(this);
现在这个线程就像这样:
(ac).runOnUiThread(new Runnable() {
@Override
public void run() {});
我强烈建议你像我一样做。
关于android - 如何在 Timer 中使用 LocationManager?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22878717/