I'm dealing with a weird permission issue.
I'm trying to read contacts, SMS and call logs and for that, I'm using these permissions in my 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" />
When I first started to read sms, it worked. Then I tried contacts and I had an error message telling me that I need READ_CONTACTS permission (which I already used). I cleaned the project and then it worked. But now nothing works, be it SMS, contacts or call logs. I cleaned the project, rebuild it, still nothing.
My gradle build:
android {
compileSdkVersion 23
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.xyz"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
I don't think the minSdkVerion or compileSdkVersion is a problem, like I said at the beginning SMS reading worked, now it's not even if I remove the call log permission.
Some code lines:
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;
}
I'm calling these methods with 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;
}
Thanks for your help!