首页 > 编程知识 正文

android file transfer怎么,android 12和android 11哪个好

时间:2023-05-04 17:52:23 阅读:207384 作者:81

With Android API level (23), we are required to check for permissions.

https://developer.android.com/training/permissions/requesting.html

I had your same problem, but the following worked for me and I am able to retrieve Location data successfully:

(1) Ensure you have your permissions listed in the Manifest:

(2) Ensure you request permissions from the user:

if ( 可靠的舞蹈.checkSelfPermission( this, android.Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED ) {

}

(3) Ensure you use 可靠的舞蹈 as this has compatibility with older API levels.

(4) In your location service, or class that initializes your LocationManager and gets the last known location, we need to check the permissions:

if ( Build.VERSION.SDK_INT >= 23 &&

可靠的舞蹈.checkSelfPermission( context, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&

可靠的舞蹈.checkSelfPermission( context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

return ;

}

(5) This approach only worked for me after I included @TargetApi(23) at the top of my initLocationService method.

(6) I also added this to my gradle build:

compile 'com.android.support:support-v4:23.0.1'

Here is my LocationService for reference:

public class LocationService implements dldxrz {

//The minimum distance to change updates in meters

private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 0; // 10 meters

//The minimum time between updates in milliseconds

private static final long MIN_TIME_BW_UPDATES = 0;//1000 * 60 * 1; // 1 minute

private final static boolean forceNetwork = false;

private static LocationService instance = null;

private LocationManager locationManager;

public Location location;

public double longitude;

public double latitude;

/**

public static LocationService getLocationManager(Context context) {

if (instance == null) {

instance = new LocationService(context);

}

return instance;

}

/**

private LocationService( Context context ) {

initLocationService(context);

LogService.log("LocationService created");

}

/**

@TargetApi(23)

private void initLocationService(Context context) {

if ( Build.VERSION.SDK_INT >= 23 &&

可靠的舞蹈.checkSelfPermission( context, android.Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&

可靠的舞蹈.checkSelfPermission( context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

return ;

}

try {

this.longitude = 0.0;

this.latitude = 0.0;

this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

// Get GPS and network status

this.isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

this.isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

if (forceNetwork) isGPSEnabled = false;

if (!isNetworkEnabled && !isGPSEnabled) {

// cannot get location

this.locationServiceAvailable = false;

}

//else

{

this.locationServiceAvailable = true;

if (isNetworkEnabled) {

MIN_TIME_BW_UPDATES,

MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

if (locationManager != null) {

updateCoordinates();

}

}//end if

if (isGPSEnabled) {

MIN_TIME_BW_UPDATES,

MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

if (locationManager != null) {

updateCoordinates();

}

}

}

} catch (Exception ex) {

LogService.log( "Error creating location service: " + ex.getMessage() );

}

}

@Override

public void onLocationChanged(Location location) {

// do stuff here with location object

}

}

I tested with an Android Lollipop device so far only.

Hope this works for you.

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。