onLocationChanged事件。
这似乎取决于附加的looper:
locManager.requestSingleUpdate("network",locListener,Looper.getMainLooper());
locManager.requestSingleUpdate("network",locListener,null);
locManager.requestSingleUpdate("network",locListener,Looper.myLooper());
没有给模拟器和设备提供相同的结果!
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();
}
};
}
感谢所有人。