android - How to give the runtime permission for read the OTP number for marshmallow? -
this class read otp number works fine in devices in marshmallow doesn't read otp number. know resolving issue wants runtime permission don't know how can give runtime permission read otp number sent server.
public class incominmsg extends broadcastreceiver { final smsmanager smsmanager = smsmanager.getdefault(); public static string general_otp_template = "(.*) otp reset password app. treat confidential"; public static final string key_prefernce = "prefernce"; public static final string key_otp = "otp"; string otp; sharedpreferences sharedpreferences; sharedpreferences.editor editor; private final int request_code_ask_for_permissions = 1234; @override public void onreceive(context context, intent intent) { final bundle bundle = intent.getextras(); int permissioncheck = contextcompat.checkselfpermission(context, android.manifest.permission.read_sms); sharedpreferences = context.getsharedpreferences(key_prefernce, context.mode_private); editor = sharedpreferences.edit(); activity activity = (activity) context; try { if (bundle != null) { final object[] pdusobj = (object[]) bundle.get("pdus"); smsmessage[] smsmessages = new smsmessage[pdusobj.length]; (int = 0; < pdusobj.length; i++) { smsmessages[i] = smsmessage.createfrompdu((byte[]) pdusobj[i]); string message = smsmessages[i].getdisplaymessagebody(); } pattern generalotppattern = pattern.compile(general_otp_template); matcher generalotpmatcher = generalotppattern.matcher(smsmessages[0].getmessagebody().tostring()); if (generalotpmatcher.find()) { permissioncheck = contextcompat.checkselfpermission(context, android.manifest.permission.read_sms); if (permissioncheck != packagemanager.permission_granted) { activitycompat.requestpermissions(activity, new string[]{android.manifest.permission.read_sms}, request_code_ask_for_permissions); return; } otp = generalotpmatcher.group(1);//this otp code editor.clear(); editor.putstring(key_otp, otp); editor.commit(); } } } catch (exception e) { } } }
you can ask read_sms
permission @ run-time -
final private int request_code_ask_for_permissions = 752136975889; private void askforreadsmspermission() { int hasreadsmspermission = checkselfpermission(manifest.permission.read_sms); if (hasreadsmspermission != packagemanager.permission_granted) { requestpermissions(new string[] {manifest.permission.read_sms}, request_code_ask_for_permissions); return; } //your code read otp }
p.s. not forget add <uses-permission-sdk-23/>
in androidmanifest.xml
Comments
Post a Comment