android - Runtime permission with 2 request in the same activity -
i'm trying ask 2 runtime permissions int same activity. first 1 trigger button , ask read_contacts, if allowed go forward via onactivityresult. other runtime permission ask send_sms , if allowed go onrequestpermissionsresult. now, understand how workd - i'm doing wrong. 2 runtime permissions need run through 1 of choices , need build method switch case or handle 2 permissions won't crash. need idea how handle 2 events won't allow contacts permission , activity try run sms method.
that's onactivityresult override can't rid of.
@override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); try { if (requestcode == request_code_pick_contacts && resultcode == result_ok) { log.d(tag, "response: " + data.tostring()); uricontact = data.getdata(); retrievecontactnumber(); } } catch (exception e){ e.getstacktrace(); toast.maketext(smsactivity.this, "hi, tried :p", toast.length_short).show(); } } private void retrievecontactnumber() { cursor cursorid = getcontentresolver().query(uricontact, new string[]{contactscontract.contacts._id}, null, null, null); if (cursorid.movetofirst()) { contactid = cursorid.getstring(cursorid.getcolumnindex(contactscontract.contacts._id)); } cursorid.close(); log.d(tag, "contact id: " + contactid); cursor cursorphone = getcontentresolver().query(contactscontract.commondatakinds.phone.content_uri, new string[]{contactscontract.commondatakinds.phone.number}, contactscontract.commondatakinds.phone.contact_id + " = ? , " + contactscontract.commondatakinds.phone.type + " = " + contactscontract.commondatakinds.phone.type_mobile, new string[]{contactid}, null); if (cursorphone.movetofirst()) { contactnumber = cursorphone.getstring(cursorphone.getcolumnindex(contactscontract.commondatakinds.phone.number)); } cursorphone.close(); txtcbnum = (edittext) findviewbyid(r.id.txtnum); txtcbnum.settext(contactnumber, textview.buffertype.editable); log.d(tag, "contact phone number: " + contactnumber); } private void requestsmspermission() { if (contextcompat.checkselfpermission(smsactivity.this, manifest.permission.send_sms) != packagemanager.permission_granted) { activitycompat.requestpermissions(smsactivity.this, new string[]{manifest.permission.send_sms}, permission_send_sms); } else { contactnumber = txtcbnum.gettext().tostring(); sendsms(contactnumber, dibur); } } @override public void onrequestpermissionsresult(int requestcode, string permissions[], int[] grantresults) { if (requestcode == permission_send_sms) { contactnumber = txtcbnum.gettext().tostring(); sendsms(contactnumber, dibur); } else if (requestcode == my_permissions_request_read_contacts) { try { // need insert contact number retrieve here } catch (exception e) { e.getstacktrace(); toast.maketext(smsactivity.this, "hi, tried :p", toast.length_short).show(); } } else { toast.maketext(smsactivity.this, "permission denied, can't send sms.", toast.length_long).show(); } } private void sendsms(string phonenumber, string message) { try { smsmanager sms = smsmanager.getdefault(); sms.sendtextmessage(phonenumber, null, message, null, null); toast.maketext(smsactivity.this, "sms sent " + contactnumber, toast.length_short).show(); } catch (exception e) { toast.maketext(smsactivity.this, "sms failed, please try again." + contactnumber, toast.length_long).show(); e.printstacktrace(); } }
you can tell 2 requestpermissions() calls apart integer passing second parameter. value supplied onrequestpermissionsresult() parameter.
so, example, if in 1 place have:
activitycompat.requestpermissions(this, ..., result_perms_record_video); (with array of permission names ...)
and in place have:
activitycompat.requestpermissions(this, ..., result_perms_take_picture); (with, presumably, different array of permission names ...)
then can have:
@override public void onrequestpermissionsresult(int requestcode, string[] permissions, int[] grantresults) { if (requestcode==result_perms_take_picture) { // } else if (requestcode==result_perms_record_video) { // else } }
Comments
Post a Comment