Ste*_*ven 9 permissions android contacts
我正在尝试从ContactsContractURI中读取联系人姓名,电话号码和电子邮件,SecurityException当我尝试运行该程序时,我得到了一个.我在AndroidManifest.xml文件中设置了权限:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.smumn.cs394"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
**<uses-permission android:name="android.pemission.READ_CONTACTS"/>**
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ReadPhoneNumbers"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>`
Run Code Online (Sandbox Code Playgroud)
以下是应用程序代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_list);
ContentResolver resolver = getContentResolver();
Cursor c = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
//[...] Work through data here`
Run Code Online (Sandbox Code Playgroud)
我在最后一行(resolver.query())得到一个安全异常:
`03-08 07:41:40.812: ERROR/AndroidRuntime(416): FATAL EXCEPTION: main
03-08 07:41:40.812: ERROR/AndroidRuntime(416): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.smumn.cs394/edu.smumn.cs394.ReadPhoneNumbers}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts from pid=416, uid=10037 requires android.permission.READ_CONTACTS
[...]
03-08 07:41:40.812: ERROR/AndroidRuntime(416): Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts from pid=416, uid=10037 requires android.permission.READ_CONTACTS
[...]
03-08 07:41:40.812: ERROR/AndroidRuntime(416): at edu.smumn.cs394.ReadPhoneNumbers.onCreate(ReadPhoneNumbers.java:30)
Run Code Online (Sandbox Code Playgroud)
[...]`
我必须遗漏一些东西,但我无法弄清楚是什么.
kri*_*nan 16
在运行时请求权限
从Android 6.0(API级别23)开始,用户在应用程序运行时向应用程序授予权限,而不是在安装应用程序时.
如果您需要添加的权限未列在正常权限下,则需要处理"运行时权限".运行时权限是应用程序运行时所需的权限.这些权限将向用户显示一个对话框,类似于以下内容:
添加"运行时权限"的第一步是将其添加到AndroidManifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.codepath.androidpermissionsdemo" >
<uses-permission android:name="android.permission.READ_CONTACTS" />
...
</manifest>
Run Code Online (Sandbox Code Playgroud)
接下来,您需要启动权限请求并处理结果.以下代码显示了如何在Activity的上下文中执行此操作,但也可以在Fragment中执行此操作.
// MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// In an actual app, you'd want to request a permission when the user performs an action
// that requires that permission.
getPermissionToReadUserContacts();
}
// Identifier for the permission request
private static final int READ_CONTACTS_PERMISSIONS_REQUEST = 1;
// Called when the user is performing an action which requires the app to read the
// user's contacts
public void getPermissionToReadUserContacts() {
// 1) Use the support library version ContextCompat.checkSelfPermission(...) to avoid
// checking the build version since Context.checkSelfPermission(...) is only available
// in Marshmallow
// 2) Always check for permission (even if permission has already been granted)
// since the user can revoke permissions at any time through Settings
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// The permission is NOT already granted.
// Check if the user has been asked about this permission already and denied
// it. If so, we want to give more explanation about why the permission is needed.
if (shouldShowRequestPermissionRationale(
Manifest.permission.READ_CONTACTS)) {
// Show our own UI to explain to the user why we need to read the contacts
// before actually requesting the permission and showing the default UI
}
// Fire off an async request to actually get the permission
// This will show the standard permission request dialog UI
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},
READ_CONTACTS_PERMISSIONS_REQUEST);
}
}
// Callback with the request from calling requestPermissions(...)
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[],
@NonNull int[] grantResults) {
// Make sure it's our original READ_CONTACTS request
if (requestCode == READ_CONTACTS_PERMISSIONS_REQUEST) {
if (grantResults.length == 1 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Read Contacts permission granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Read Contacts permission denied", Toast.LENGTH_SHORT).show();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 9
确保将其添加到应用程序标记之外.在Ubuntu上使用Eclipse开发2.3.3的目标平台时,我在日志文件中有权限失败,表明在处理类似的事情时我需要这个确切的行.直到我将*uses-permission ... READ_CONTACTS*行移到应用程序标记之外才能工作.
Hello Steven调试日志跟踪告诉您需要...需要android.permission.READ_CONTACTS
所以只需通过编辑Manifest.xml来尝试添加其他权限,看看它是否被正确加入.
并检查此行没有**
<uses-permission android:name="android.permission.READ_CONTACTS" />
Run Code Online (Sandbox Code Playgroud)
担
小智 5
使用api 23,权限<uses-permission android:name="android.pemission.READ_CONTACTS"/>不起作用,在api 22(棒棒糖)或更低版本的模拟器中更改api级别
| 归档时间: |
|
| 查看次数: |
16906 次 |
| 最近记录: |