-1

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!

Kiritsu
  • 51
  • 1
  • 3
  • 2
    See https://stackoverflow.com/questions/32635704/cant-get-the-permission, then either drop your `targetSdkVersion` to `22` or add support for runtime permissions to your code. – CommonsWare Mar 28 '16 at 21:46

2 Answers2

0

Thank you for your quick answers, especially @CommonsWare. I haven't found this topic, I found others (but older unfortunately). I used checkSelfPermission() and requestPermissions() and overrided onRequestPermissionsResult(). Now it works well. I'm writing my code here so that everyone dealing with that issue can understand how it works.

Here is my 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);
        }
    }

And the method I'm calling before every content provider:

int hasReadContactsPermission = checkSelfPermission(mContext, permission);
        if (hasReadContactsPermission != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(this.activity, new String[]{permission},
                    REQUEST_CODE_ASK_PERMISSIONS);
            return;
        }
Kiritsu
  • 51
  • 1
  • 3
-1

Try to move the order of the permissions around. I would try placing the Read_Contacts permission over Read_SMS

Ethan
  • 1,905
  • 2
  • 21
  • 50
  • The order of the `` elements has no bearing on their use. So long as they are immediate children of the root `` element, the order in which they appear is immaterial. – CommonsWare Mar 28 '16 at 22:24
  • I am aware of this, but I suffered from a similar bug a year or so back ad this is the method I used to fix it. Please question the answer next time before automatically downing it. – Ethan Mar 28 '16 at 23:11
  • Something else fixed your problem. This did not. If you edit your question and provide a reproducible test case showing that the order of `` elements matters, I will happily retract my downvote. – CommonsWare Mar 28 '16 at 23:13
  • @CommonsWare you know that I can't, I was using eclipse back then. Not going to start it up again to prove this to you. – Ethan Mar 28 '16 at 23:18
  • I tried this method (changing the permissions' order) before opening this topic but it did not change anything. – Kiritsu Mar 28 '16 at 23:20