requestSingleUpdate侦听器不起作用

时间:2015-11-28 15:39:49

标签: android geolocation emulation android-runonuithread android-looper

使用requestSingleUpdate不会触发

onLocationChanged事件。

这似乎取决于附加的looper:

locManager.requestSingleUpdate("network",locListener,Looper.getMainLooper());
locManager.requestSingleUpdate("network",locListener,null);
locManager.requestSingleUpdate("network",locListener,Looper.myLooper());

没有给模拟器和设备提供相同的结果!

问题

  • 如果多次调用requestSingleUpdate(使用不同的提供者),只会进行最后一次查询?
  • 必须在ui线程上调用requestSingleUpdate?如果没有,有一个把它叫到外面的技巧?

完整班级:

public class LocationNew {

    private Context context;


    public LocationNew(Context context) {
        this.context = context;
    }

    public void requestNew(Looper paramLooper){

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int permissionLocation = ContextCompat.checkSelfPermission(DooodApp.getContext(), Manifest.permission.ACCESS_FINE_LOCATION);
            if (permissionLocation != PackageManager.PERMISSION_GRANTED) {
                return;
            }
        }

        LocationManager locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        List<String> listProv = locManager.getProviders(true);
        for (String provTmp: listProv) {
            locManager.requestSingleUpdate(provTmp,locListener,Looper.getMainLooper());
        }
    }

    private LocationListener locListener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {
            Toast.makeText(context, "New location from : " + location.getProvider(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Toast.makeText(context, "onStatusChanged" , Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onProviderEnabled(String provider) {
            Toast.makeText(context, "onProviderEnabled" , Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onProviderDisabled(String provider) {
            Toast.makeText(context, "onProviderDisabled" , Toast.LENGTH_SHORT).show();
        }
    };
}

感谢所有人。

0 个答案:

没有答案