3

使用 requestSingleUpdate 不会触发 onLocationChanged 事件。

似乎它取决于附加的活套前:

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

模拟器和设备的结果不一样!

问题

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

完整类:

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();
        }
    };
}

谢谢大家。

4

0 回答 0