我正处理一个奇怪的许可问题。
我试图阅读联系人,短信和通话记录,为此,我在我的Manifest中使用了这些权限:
<uses-permission android:name="android.permission.WRITE_CALL_LOG" />
<uses-permission android:name="android.permission.READ_CALL_LOG" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
当我第一次开始阅读短信时,它起作用了。然后我尝试了联系人,我收到一条错误消息,告诉我需要READ_CONTACTS权限(我已经使用过)。我清理了项目然后它工作了。但现在没有任何作用,无论是短信,联系人还是通话记录。我清理了项目,重建了它,仍然没有。
我的gradle构建:
android {
compileSdkVersion 23
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.xyz"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
我不认为minSdkVerion或compileSdkVersion是一个问题,就像我在开始时说的短信阅读工作一样,现在即使我删除了通话记录权限也不行。
一些代码行:
public void backUpContacts() {
String[] dataContacts = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone._ID};
Cursor cursor = this.mContext.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, dataContacts, null, null, null);
JSONObject jsonContacts;
JSONArray jsonArray = new JSONArray();
while (cursor != null && cursor.moveToNext()) {
jsonContacts = new JSONObject();
for (int i = 0; i < cursor.getColumnCount(); i++) {
jsonContacts.put(cursor.getColumnName(i), cursor.getString(i));
}
contacts = cursor.getColumnCount();
jsonArray.add(jsonContacts);
}
Log.d("jsonArray", jsonArray.toString());
if (cursor != null) cursor.close();
createFile(jsonArray, "contacts");
}
public void backupSms() {
JSONArray listSmsInbox;
JSONArray listSmsSent;
listSmsInbox = getPhoneSms("content://sms/inbox", "received");
listSmsSent = getPhoneSms("content://sms/sent", "sent");
Log.i("listSmsInbox", listSmsInbox.toString());
Log.i("listSmsSent", listSmsSent.toString());
}
private JSONArray getPhoneSms(String parsing, String param) {
Cursor c = this.mContext.getContentResolver().query(Uri.parse(parsing), null, null, null, null);
JSONObject jsonSms = new JSONObject();
JSONArray jsonArray = new JSONArray();
while (c != null && c.moveToNext()) {
jsonSms = new JSONObject();
for (int i = 0; i < c.getColumnCount(); i++) {
jsonSms.put(c.getColumnName(i), c.getString(i));
}
jsonArray.add(jsonSms);
if (param.equals("received")) {
receivedSms = c.getColumnCount();
} else {
sentSms = c.getColumnCount();
}
}
jsonArray.add(jsonSms);
if (c != null) c.close();
createFile(jsonArray, param);
return jsonArray;
}
我用AsyncTask调用这些方法:
@Override
protected Void doInBackground(Void... params) {
if (param.equals("contacts")) {
backUpContent.backUpContacts();
} else if (param.equals("sms")) {
backUpContent.backupSms();
} else if (param.equals("callLogs")) {
backUpContent.backUpCallLogs();
}
publishProgress(0);
return null;
}
感谢您的帮助!
答案 0 :(得分:0)
感谢您的快速解答,尤其是@CommonsWare。我还没有找到这个话题,我找到了其他人(但不幸的是老了)。我使用了checkSelfPermission()和requestPermissions()并覆盖了onRequestPermissionsResult()。现在它运作良好。我在这里编写代码,以便处理该问题的每个人都能理解它是如何工作的。
这是我的onRequestPermissionsResult():
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_ASK_PERMISSIONS: // final int variable = 123
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
myMethodHere();
return;
} else {
// Permission Denied
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT)
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
我在每个内容提供商之前调用的方法:
int hasReadContactsPermission = checkSelfPermission(mContext, permission);
if (hasReadContactsPermission != PackageManager.PERMISSION_GRANTED) {
requestPermissions(this.activity, new String[]{permission},
REQUEST_CODE_ASK_PERMISSIONS);
return;
}
答案 1 :(得分:-1)
尝试移动权限的顺序。我会尝试将Read_Contacts
权限置于Read_SMS