0

我有这个代码来获取我的 GPS 位置:

    public void run() {

        mLocationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);

        if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

            Looper.prepare();

            mLocationListener = new MyLocationListener();
            mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);

            Looper.loop(); 
            Looper.myLooper().quit(); 

        } else {

            //Never be here!!!

        }
}

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        pd.dismiss();
        mLocationManager.removeUpdates(mLocationListener);

        if (currentLocation!=null) {

                  //found. OK
        }
    }
};

private class MyLocationListener implements LocationListener {
    @Override
    public void onLocationChanged(Location loc) {
        if (loc != null) {
            setCurrentLocation(loc);
            handler.sendEmptyMessage(0);

        }
    }

    @Override
     public void onProviderDisabled(String provider) {
         //If I disable de GPS I have an error
     }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
    }
} 

如何检测 GPS 已禁用且查找数据的时间太长?

我认为 onProviderDisabled 检测到该事件,但是,我的代码永远不会去那里。我得到了这个例外:

无法在尚未调用 Looper.prepare 的线程内创建处理程序

先感谢您

4

0 回答 0