
我正在尝试检查应用的权限,目前正在关注Android开发者培训.
但是,在插入page中列出的代码时遇到了问题.
该页面声称MY_PERMISSIONS_REQUEST是一个定义为常量的应用程序.但是,当我把它放在我的实际应用程序中时,它说它没有定义…
有人可以解释为什么会这样吗?
谢谢!
示例代码:
// Here, thisActivity is the current activityif (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { // Should we show an explanation? if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, Manifest.permission.READ_CONTACTS)) { // Show an expanation to the user *asynchronously* -- don't block // this thread waiting for the user's response! After the user // sees the explanation, try again to request the permission. } else { // No explanation needed, we can request the permission. ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.READ_CONTACTS}, MY_PERMISSIONS_REQUEST_READ_CONTACTS); // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an // app-defined int constant. The callback method gets the // result of the request. }}解决方法:
您可以将其定义为字段常量,如下所示:
private static final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 1;// Here, thisActivity is the current activityif (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { ...这是因为可能还有其他请求.您可以通过您定义的请求代码将其与其他代码区分开来.
总结以上是内存溢出为你收集整理的android – MY_PERMISSONS_REQUEST没有定义?全部内容,希望文章能够帮你解决android – MY_PERMISSONS_REQUEST没有定义?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)