0

I am developing an application that required to access contacts in phone. this method is executed inside a thread. But when app start for the first time it crashes and says android.permission.READ_CONTACTS or android.permission.READ_CONTACTS required as error. as soon as press on OK in on error pop up dialog box. it restart it self asking for permission and works fine. In some phones it's not showing any errors or dialog box.

here's the code inside fragment on onCreateView method to check whether permission already has been granted

if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)
                == PackageManager.PERMISSION_GRANTED) {
            Runnable r = new Runnable() {
                @Override
                public void run() {
                    getContacts();
                }
            };
            Thread thread = new Thread(r);
            thread.start();
        }else{
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (shouldShowRequestPermissionRationale(Manifest.permission.READ_CONTACTS)) {
                    Toast.makeText(this, "Read contacts permission is required to function app correctly", Toast.LENGTH_LONG)
                            .show();
                }
                requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_READ_CONTACTS);
            }
        }

Here's the onRequestPermissionsResult method.

 @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case REQUEST_READ_CONTACTS :
                Runnable r = new Runnable() {
                    @Override
                    public void run() {
                        getContacts();
                    }
                };
                Thread thread = new Thread(r);
                thread.start();
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

Here's AndroidManifest.xml code.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.shan.chathuranga.smsscheduler">

    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />

</manifest>

What am I doing wrong. works fine in below marshmallow. requestPermissions method.

Here's the link to the repository https://chathurangaJ@bitbucket.org/chathurangaJ/sms-scheduler.git I'm stuck in this for 2 weeks please help.

I try to debug it. This error get as soon as application reach

3 Answers3

0

If open menu and long press your app then show to options 1.app info and 2.uninstall,just drag your app to app info and then you will see the storage and data usage and etc.., then see the permissions option open hit and check contact permission present or not,if there is contact just enable and run you app you will get contact,Or otherwise your code may be have a problem and if you have any permission detail please check below link, https://developer.android.com/training/permissions/requesting.html

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Vadivel
  • 780
  • 1
  • 6
  • 21
0
private boolean checkPermission(){
        if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.READ_CONTACTS)) {
                Toast.makeText(getActivity(), "Contact read permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
                Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", getActivity().getPackageName(), null));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivityForResult(intent, 789);
                return false;
            }else {
               requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 123);
                return false;
            }
        }
        return true;
    }

use this method to check run time permission for API level 23 or higher, it will show you a dialog for permission at run time if user allow then all is okay if deny then you have to recheck for permission again to prevent from app crash,

before using this code please add Contact READ permission as you already added. hopefully it will help you.

NOTE

  1. In AppCompatActivity use the method ActivityCompat.requestpermissions
  2. In v4 support fragment you should use requestpermissions
  3. Catch is if you call AppcompatActivity.requestpermissions in your fragment then callback will come to activity and not fragment

4.Make sure to call super.onRequestPermissionsResult from the activity's onRequestPermissionsResult.

  • Well, I'm using this method in fragment and I've use `requestpermissions` method. It's not working. just to make sure I even use `AppcompatActivity.requestpermissions` to see whether dialog box pop up. But it still not working. I'm using runtime permission for sending sms in application that works fine but this. – Chathuranga Shan Jayarathna Nov 17 '16 at 06:51
0

this your method and working fine i have check it please try same and call from onCreateView method.

private void check(){


        if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_CONTACTS)
                == PackageManager.PERMISSION_GRANTED) {
            Runnable r = new Runnable() {
                @Override
                public void run() {
//                    getContacts();
                }
            };
            Thread thread = new Thread(r);
            thread.start();
        }else{
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (shouldShowRequestPermissionRationale(Manifest.permission.READ_CONTACTS)) {
                    Toast.makeText(getActivity(), "Read contacts permission is required to function app correctly", Toast.LENGTH_LONG).show();
                }else {
                    ActivityCompat.requestPermissions(getActivity(),
                            new String[]{Manifest.permission.READ_CONTACTS},
                            1);                }

            }
        }


    }

and make sure you have apply usage permission in AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="xyz">

    <uses-permission android:name="android.permission.READ_CONTACTS"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

And call it from :

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_contact, container, false);

        check();


        return view;
    }