可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am developing an location app which is working fine in all devices except in Marshmallow.I have requested permission during runtime and when I grant permission longitude and latitude is not fetched,if i go to settings and change the location from high accuracy to battery saving,location is fetched and the app works.I want location to be fetched at high accuracy.
ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.CAMERA, android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.INTERNET, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE, Manifest.permission.WAKE_LOCK, }, 1);
回答1:
try this
step 1 :- add this permission in manifiesr file
android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION,
step 2 : ask runtime permission
String permission = android.Manifest.permission.ACCESS_FINE_LOCATION; if (ActivityCompat.checkSelfPermission(SearchCityClass.this, permission) != PackageManager.PERMISSION_GRANTED && ActivityCompat. checkSelfPermission(SearchCityClass.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(SearchCityClass.this, new String[] {permission}, PERMISSION_GPS_CODE); }
step 3: handle permsiion result
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == PERMISSION_GPS_CODE) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, location_permission_granted_msg, Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, location_permission_not_granted_msg, Toast.LENGTH_SHORT).show(); } } }
回答2:
You have to give run time permission for android 6 like this
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { // Permission already Granted //Do your work here //Perform operations here only which requires permission } else { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1); }
and if permission is not already granted override onRequestPermission Results like this
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == 1) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { //Permission Granted //Do your work here //Perform operations here only which requires permission } } }
回答3:
Add this code your Activity class
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_images); requestLocationPermission(); //your other codes } private void requestLocationPermission() { if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { } else { ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { //Checking the request code of our request //If permission is granted if (requestCode == 1) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { //Displaying a toast Toast.makeText(this, "Permission granted now you can get the location", Toast.LENGTH_LONG).show(); } else { //Displaying another toast if permission is not granted Toast.makeText(this, "Oops you just denied the permission", Toast.LENGTH_LONG).show(); } }
Add this on your Mainfest XML
回答4:
that's problem of permission
if (Build.VERSION.SDK_INT >= 23) { if (checkPermission(Manifest.permission.ACCESS_FINE_LOCATION, getActivity().getApplicationContext(), getActivity())) { if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { navigateToGoogleMap(); } else { showSettingsAlert(); } } else { requestPermission(Manifest.permission.ACCESS_FINE_LOCATION, PERMISSION_REQUEST_CODE, getActivity().getApplicationContext(), getActivity()); } } else { if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { navigateToGoogleMap(); } else { showSettingsAlert(); } }