android------关于API 23的权限问题
来源:互联网 发布:苹果怎么打开php文件 编辑:程序博客网 时间:2024/06/08 01:14
在代码:
locationManager.requestLocationUpdates(provider, 60000, 50, getListener());中报以下的错误:
Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with `checkPermission`) or explicitly handle a potential `SecurityException`
在AndroidManifest.xml中加入了以下的权限还是有错:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>百度了下,大意是:
调用需要的权限可能被用户拒绝:代码应该显式地检查权限是否可用(用checkPermission)或显式地处理一个潜在的“SecurityException”
这是在使用 Android marshmallow(API 23)时出现的
参考以下代码解决:
public class MainActivity extends AppCompatActivity { /* GPS Constant Permission */ private static final int MY_PERMISSION_ACCESS_COARSE_LOCATION = 11; private static final int MY_PERMISSION_ACCESS_FINE_LOCATION = 12; /* Position */ private static final int MINIMUM_TIME = 10000; // 10s private static final int MINIMUM_DISTANCE = 50; // 50m /* GPS */ private String mProviderName; private LocationManager mLocationManager; private LocationListener mLocationListener; @Override protected void onCreate(Bundle savedInstanceState) { ... mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // Get the best provider between gps, network and passive Criteria criteria = new Criteria(); mProviderName = mLocationManager.getBestProvider(criteria, true); // API 23: we have to check if ACCESS_FINE_LOCATION and/or ACCESS_COARSE_LOCATION permission are granted 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) { // No one provider activated: prompt GPS if (mProviderName == null || mProviderName.equals("")) { startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } // At least one provider activated. Get the coordinates switch (mProviderName) { case "passive": mLocationManager.requestLocationUpdates(mProviderName, MINIMUM_TIME, MINIMUM_DISTANCE, this); Location location = mLocationManager.getLastKnownLocation(mProviderName); break; case "network": break; case "gps": break; } // One or both permissions are denied. } else { // The ACCESS_COARSE_LOCATION is denied, then I request it and manage the result in // onRequestPermissionsResult() using the constant MY_PERMISSION_ACCESS_FINE_LOCATION if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSION_ACCESS_COARSE_LOCATION); } // The ACCESS_FINE_LOCATION is denied, then I request it and manage the result in // onRequestPermissionsResult() using the constant MY_PERMISSION_ACCESS_FINE_LOCATION if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ) { ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.ACCESS_FINE_LOCATION }, MY_PERMISSION_ACCESS_FINE_LOCATION); } } } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSION_ACCESS_COARSE_LOCATION: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted } else { // permission denied } break; case MY_PERMISSION_ACCESS_FINE_LOCATION: { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission was granted } else { // permission denied } break; } } }}
1 0
- android------关于API 23的权限问题
- Android API 23+上的权限问题
- android关于权限的问题
- 关于android的权限问题
- Android 6.0(API 23)后的权限问题
- 关于android读写sdcard的权限问题
- 关于Android权限被禁止的问题
- 关于Android权限问题的一点思考
- Android关于华为手机的权限问题
- 关于android 6.0权限问题
- 关于Android App权限问题
- 关于API < 23的权限适配解决问题
- 关于API的问题
- 关于API的问题!
- Android权限检查API checkSelfPermission失效问题
- Android权限检查API checkSelfPermission失效问题
- Android中关于Manifest的权限注册问题
- 关于Unity打Android包自动添加权限的问题
- 将链表链接并且对链表排序(通过插入和删除函数实现
- 一个很好的仿微博消息浏览功能的Demo
- 求Sn=a+aa+aaa+aaaa+...+aaaaaa...a的值
- 在Java的JDBC使用中设置事务回滚的保存点的方法
- C#反射实用技术
- android------关于API 23的权限问题
- Android 开发之集成百度地图的定位与地图展示
- uva10943 How do you add?
- Leetcode-1.Two Sum
- 设计模式
- 内网ip打洞-----p2p实现原理
- linux上操作注意事项
- hdu 4533 线段树(问题转化+)
- Toast不是乱弹的