Android applications obtain permission cannot run normally -
wrote contacts , contact phone number , displayed in small program, after permission in android 6 cannot display data, restart program read properly. think should there access out of question, because after restart authority need not ask when can read properly, , need pop-up dialog asking while allowing access permissions still can't show, after restart.
public class mainactivity extends appcompatactivity { listview contactsview; arrayadapter<string> adapter; list<string> contactslist = new arraylist<string>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); contactsview = (listview) findviewbyid(r.id.contacts_view); adapter = new arrayadapter<string>(this, android.r.layout. simple_list_item_1, contactslist); contactsview.setadapter(adapter); insertdummycontactwrapper(); } final private int request_code_ask_permissions = 123; private void insertdummycontactwrapper() { int haswritecontactspermission = checkselfpermission(manifest.permission.read_contacts); if (haswritecontactspermission != packagemanager.permission_granted) { requestpermissions(new string[] {manifest.permission.read_contacts}, request_code_ask_permissions); return; } readcontacts(); } @override public void onrequestpermissionsresult(int requestcode, string[] permissions, int[] grantresults) { switch (requestcode) { case request_code_ask_permissions: if (grantresults[0] == packagemanager.permission_granted) { // permission granted readcontacts(); } else { // permission denied toast.maketext(mainactivity.this, "read_contacts denied", toast.length_short) .show(); } break; default: super.onrequestpermissionsresult(requestcode, permissions, grantresults); } } public void readcontacts() { cursor cursor = null; try { cursor = getcontentresolver().query(contactscontract.commondatakinds.phone.content_uri, null, null, null, null); while (cursor.movetonext()) { string displayname = cursor.getstring(cursor.getcolumnindex(contactscontract. commondatakinds.phone.display_name)); string number = cursor.getstring(cursor.getcolumnindex(contactscontract. commondatakinds.phone.number)); contactslist.add(displayname + "\n" + number); } } catch (exception e) { e.printstacktrace(); } finally { if (cursor != null) { cursor.close(); } } }
Comments
Post a Comment