Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何从LocationManager获得一个带超时的单一位置修复?_Android_Locationmanager - Fatal编程技术网

Android 如何从LocationManager获得一个带超时的单一位置修复?

Android 如何从LocationManager获得一个带超时的单一位置修复?,android,locationmanager,Android,Locationmanager,我正在使用LocationManager获取单个位置修复: public class MyActivity extends Activity { private LocationManager lm; private ProgressDialog myDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstance

我正在使用
LocationManager
获取单个位置修复:

public class MyActivity extends Activity {
    private LocationManager lm;
    private ProgressDialog myDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        LocationListener ll = new MyLocationListener();
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, ll);
        myDialog = ProgressDialog.show(this, null , "Determining Your Location", true);
    }

    class MyLocationListener implements LocationListener {
        public void onLocationChanged(Location location) {
            if (location != null) {
                ...

                lm.removeUpdates(this);
                myDialog.dismiss();
            }
        }
    }
}
因此,如果找不到位置修复程序,监听位置可能会永远持续下去。我想通过在60秒后停止监听某个位置,并向用户显示一个错误,表示无法确定他们的位置,从而为我的代码增加一些健壮性


如何才能做到这一点?

一种方法是使用处理程序并在60秒后使用postDelayed停止侦听器


您也可以使用定时器和定时器任务。当您创建处理程序以使用postDelayed时,您应该小心内存泄漏,因为处理程序在完成活动或服务事件之后仍然引用您的活动或服务事件。PostDelayed将发送到主线程中的消息队列。因此,将处理程序设置为静态或使用弱引用

您需要考虑以下代码以避免内存泄漏。

public class MyService extends Service {

   private static class MyHandler extends Handler {
      WeakReference<MyService> ref;

      public MyHandler(MyService r) {
         ref = = new WeakReference<MyService>(r);
      }

      public void handleMessage(Message msg) {

         MyService service = ref.get();
         if (servic==null) {
            return;
         }

         //Do something here
     }

   }

}
公共类MyService扩展服务{
私有静态类MyHandler扩展了Handler{
WeakReference-ref;
公共MyHandler(MyService r){
ref==新的WeakReference(r);
}
公共无效handleMessage(消息消息消息){
MyService service=ref.get();
if(servic==null){
返回;
}
//在这里做点什么
}
}
}
您可以使用用户a,也可以通过调用
LocationManager来实现超时。removeUpdates
停止侦听

计时器
会创建一个新线程,这可能是过度杀戮。
AlarmManager
的文档表明“(f)或正常计时操作(滴答声、超时等)使用处理程序更容易、更高效”。
Handler
的文档描述了
Handler
的一个主要用途是“将消息和可运行程序安排为(sic)将来的某个时间点执行。”

所有符号都指向
处理程序
,认为它是实现超时的最合适的方法

public class MyActivity extends Activity implements LocationListener {
    private final int TIMEOUT = 60000;

    private LocationManager myLocationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        myLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        myLocationManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER,
            this, null);

        Runnable myRunnable = new Runnable() {
            public void run() {
                myLocationManager.removeUpdates(QualityCheckActivity.this);
                // other timeout error code
            }
        };

        Handler myHandler = new Handler();
        myHandler.postDelayed(myRunnable, TIMEOUT);
    }

    @Override
    public void onLocationChanged(Location location) {
        // location fixed code 
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) { }

    @Override
    public void onProviderEnabled(String provider) { }

    @Override
    public void onProviderDisabled(String provider) { }
}
注:

TIMEOUT
当然是以毫秒为单位的超时长度。在这种情况下,为60000毫秒或60秒

我选择在
MyActivity
本身上实现
LocationListener
接口,以便
LocationListener
Runnable
中更容易访问

而不是打电话:我正在使用它,它只提供一个位置修复

我故意没有实现
Activity.onPause
Activity.onResume
。如果活动暂停,则位置侦听和超时都将继续