简体   繁体   English

如何在Timer内部使用LocationManager?

[英]How use LocationManager inside Timer?

in my app I have a Background server and inside server class I have timer and inside timer I have a locationManager to find location : 在我的应用程序中我有一个后台服务器和内部服务器类我有计时器和内部计时器我有一个locationManager来查找位置:

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

                    LocationListener locationListener = new MyLocationListener();
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER, 5000, 10,
                            locationListener); 

but here I got this error : 但在这里我得到了这个错误:

java.lang.runtimeexception can't create handler inside thread that has not called looper.prepare();

my question here how to use locationManager inside timer ? 我在这里的问题如何在计时器内使用locationManager?

Look Background service different with Activity when you use runOnUiThread this is just possible with activity be careful OR if you use thread inside Service class you can do this like this : inside service class write this : 当你使用runOnUiThread时,查看后台服务与Activity的不同,这是可能的,活动要小心,或者如果你在Service类中使用线程,你可以这样做:内部服务类写这个:

static Activity ac;

    public static void setActivity(Activity a) {
        ac = a;

    }

and in your activity class do this : 并在您的活动类中执行此操作:

MyService.setActivity(this);

now this thread just like this : 现在这个线程就像这样:

(ac).runOnUiThread(new Runnable() {

    @Override
 public void run() {});

I strongly recommend it to you do like what I did . 我强烈建议你做我喜欢的事。

The exception message may seem arcane if you don't know what Looper threads are, but it basically means exactly what it says: you can't create a Handler (or any object that contains a Handler) in a thread that is not a Looper. 如果您不知道Looper线程是什么,则异常消息可能看似神秘,但它基本上意味着它所说的内容:您不能在不是Looper的线程中创建Handler(或包含Handler的任何对象) 。 Timer threads are not Loopers. 定时器线程不是Loopers。 Many things in Android are intended to be created only on the UI thread, which is a Looper. Android中的许多内容只能在UI线程上创建,这是一个Looper。

Trying to use a LocationListener "inside" a Timer doesn't make much sense, anyway. 无论如何,尝试在“内部”定时器使用LocationListener没有多大意义。 The LocationListener's methods are called by the system, not by your code. LocationListener的方法由系统调用,而不是由您的代码调用。 I suggest searching and reading up on event-driven programming . 我建议搜索和阅读事件驱动的编程

((Activity) getBaseContext()).runOnUiThread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

                    LocationListener locationListener = new MyLocationListener();
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER, 5000, 10,
                            locationListener); 

            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM