Solution 1 :
Add permission to the manifest.xml file:
Problem :
I want to create an android application which view phone contact in application and I wrote this code in my activity
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 1);
requestPermissions(new String[]{Manifest.permission.WRITE_CONTACTS}, 1);
requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
}
Uri u = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
Cursor curs = getContentResolver().query(u, null, null, null, null);
btn_some.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showContacts();
}
});
this code works fine with emulator but when I tried it on my phone it has stopped
logcat message is
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.MyApplication/com.example.MyApplication.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{5b66da3 28379:com.example.MyApplication/u0a264} (pid=28379, uid=10264) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
and this is my Mainfest
<uses-permission android_name="android.permission.READ_PHONE_STATE"/>
<uses-permission android_name="android.permission.READ_CONTACTS"/>
<uses-permission android_name="android.permission.WRITE_CONTACTS"/>
How can I run this application on my phone ?
Comments
Comment posted by Mohamed Hoosam
it’s already exist in it
Comment posted by developer.android.com/training/contacts-provider/…
Please take a deep look to the
Comment posted by Mohamed Hoosam
okay now it’s worked but when I open application for first time it stopped again and then I get permission message on my phone when I accept all permission application run correctly so can I solve that ?
Comment posted by Rashad Nasirli
It is worked after accepting permissions, and what did it say when stops?
Comment posted by Mohamed Hoosam
Thanks a lot I resolved this issue by override onActivityResult method and application run correctly