Android Studio 中 LocationManager 的 CheckPermissions 错误
CheckPermissions Error for LocationManager in Android Studio
我有一个很好的 GetGeoLocation 应用程序,适用于 API 级别 23(使用 checkSelfPermission)。但是,我不得不让它与 API 级别 21 兼容。因此,checkSelfPermission 没有工作,因为它是在 API 级别 23 中引入的。
所以,我更改了代码以适应这个:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CheckForCoarseLocationPermission();
}
private void CheckForCoarseLocationPermission()
{
if (android.os.Build.VERSION.SDK_INT >= 23)
{
// ANDROID 6.0 AND UP!
boolean accessCoarseLocationAllowed = false;
try
{
// Invoke checkSelfPermission method from Android 6 (API 23 and UP)
java.lang.reflect.Method methodCheckPermission = Activity.class.getMethod("checkSelfPermission", java.lang.String.class);
Object resultObj = methodCheckPermission.invoke(this, Manifest.permission.ACCESS_COARSE_LOCATION);
int result = Integer.parseInt(resultObj.toString());
if (result == PackageManager.PERMISSION_GRANTED)
{
accessCoarseLocationAllowed = true;
}
}
catch (Exception ex)
{
}
if (accessCoarseLocationAllowed)
{
Log.v("TAG","Granted");
LocationManager locationManager= (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,new LocationListener());//ERROR Happening Here!!!!!!!!!!!
}
try
{
// We have to invoke the method "void requestPermissions (Activity activity, String[] permissions, int requestCode) "
// from android 6
java.lang.reflect.Method methodRequestPermission = Activity.class.getMethod("requestPermissions", java.lang.String[].class, int.class);
methodRequestPermission.invoke(this, new String[]
{
Manifest.permission.ACCESS_FINE_LOCATION
}, 0x12345);
}
catch (Exception ex)
{
}
}
}
现在,LocationManager.requestLocationUpdate 出错了
调用需要可能被用户拒绝的权限:代码应该明确检查权限是否可用(使用 checkPermission
)或明确处理潜在的 SecurityException
less
如果我想为 API 级别 >=21 使用 LocationManager 和 CheckPermission,如何解决它。
请帮忙。
你可以把Support library放在这里使用,
if(android.support.v4.content.PermissionChecker.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED)
{
...
}
试试这个
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { // coarse permission is granted
// Todo
} else { // permission is not granted, request for permission
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)) { // show some info to user why you want this permission
Toast.makeText(this, "Allow Location Permission to use this functionality.", Toast.LENGTH_SHORT).show();
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 123 /*LOCATION_PERMISSION_REQUEST_CODE*/);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 123 /*LOCATION_PERMISSION_REQUEST_CODE*/);
}
}
注意:- 您必须在 android 清单中具有以下权限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
我有一个很好的 GetGeoLocation 应用程序,适用于 API 级别 23(使用 checkSelfPermission)。但是,我不得不让它与 API 级别 21 兼容。因此,checkSelfPermission 没有工作,因为它是在 API 级别 23 中引入的。
所以,我更改了代码以适应这个:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CheckForCoarseLocationPermission();
}
private void CheckForCoarseLocationPermission()
{
if (android.os.Build.VERSION.SDK_INT >= 23)
{
// ANDROID 6.0 AND UP!
boolean accessCoarseLocationAllowed = false;
try
{
// Invoke checkSelfPermission method from Android 6 (API 23 and UP)
java.lang.reflect.Method methodCheckPermission = Activity.class.getMethod("checkSelfPermission", java.lang.String.class);
Object resultObj = methodCheckPermission.invoke(this, Manifest.permission.ACCESS_COARSE_LOCATION);
int result = Integer.parseInt(resultObj.toString());
if (result == PackageManager.PERMISSION_GRANTED)
{
accessCoarseLocationAllowed = true;
}
}
catch (Exception ex)
{
}
if (accessCoarseLocationAllowed)
{
Log.v("TAG","Granted");
LocationManager locationManager= (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,new LocationListener());//ERROR Happening Here!!!!!!!!!!!
}
try
{
// We have to invoke the method "void requestPermissions (Activity activity, String[] permissions, int requestCode) "
// from android 6
java.lang.reflect.Method methodRequestPermission = Activity.class.getMethod("requestPermissions", java.lang.String[].class, int.class);
methodRequestPermission.invoke(this, new String[]
{
Manifest.permission.ACCESS_FINE_LOCATION
}, 0x12345);
}
catch (Exception ex)
{
}
}
}
现在,LocationManager.requestLocationUpdate 出错了
调用需要可能被用户拒绝的权限:代码应该明确检查权限是否可用(使用 checkPermission
)或明确处理潜在的 SecurityException
less
如果我想为 API 级别 >=21 使用 LocationManager 和 CheckPermission,如何解决它。
请帮忙。
你可以把Support library放在这里使用,
if(android.support.v4.content.PermissionChecker.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED)
{
...
}
试试这个
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { // coarse permission is granted
// Todo
} else { // permission is not granted, request for permission
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)) { // show some info to user why you want this permission
Toast.makeText(this, "Allow Location Permission to use this functionality.", Toast.LENGTH_SHORT).show();
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 123 /*LOCATION_PERMISSION_REQUEST_CODE*/);
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 123 /*LOCATION_PERMISSION_REQUEST_CODE*/);
}
}
注意:- 您必须在 android 清单中具有以下权限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />