执行wait()方法时,Android服务停止

时间:2017-02-27 17:19:30

标签: android service location

       Runnable run = new Runnable() {
        @Override
        public void run() {
            Looper.prepare();
            while (true) {
                manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
                info = manager.getActiveNetworkInfo();

                mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
                network_enabled = mgr.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

                if (info != null && info.isConnected()) {
                    if (network_enabled&&location!=null&&temp!=location){
                        synchronized (location) {
                            add = new Adderss(location, Main_Service.this);
                            add.Background();
                            add.onPost();
                            temp = location;
                                mgr.removeUpdates(listener);

                            try {
                                wait(1000*5);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                }
                else{
                try {
                        wait(1000*5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
             }
          }
        }
    };

    Thread location_updater = new Thread(run);
    location_updater.start();
    return Service.START_STICKY;
}

我创建了一个服务来获取用户的位置,并在获取最新位置后等待2分钟,但app在执行wait()方法时停止。 是否有任何有效的方法来获取用户当前位置。

0 个答案:

没有答案