{"id":26878,"date":"2024-03-16T07:31:14","date_gmt":"2024-03-16T07:31:14","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/"},"modified":"2024-03-22T09:39:48","modified_gmt":"2024-03-22T09:39:48","slug":"android-automatically-reads-the-sms-verification-code-and-fills-it-in","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/","title":{"rendered":"Android automatically reads the SMS verification code and fills it in."},"content":{"rendered":"<p>To automatically read SMS verification codes in an Android app and fill in the corresponding fields, you can follow these steps:<\/p>\n<ol>\n<li>Add permission: Include the permission to read SMS in the AndroidManifest.xml file.<\/li>\n<\/ol>\n<pre class=\"post-pre\"><code><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">uses-permission<\/span> <span class=\"hljs-attr\">android:name<\/span>=<span class=\"hljs-string\">\"android.permission.READ_SMS\"<\/span> \/&gt;<\/span>\r\n<\/code><\/pre>\n<ol>\n<li>Create a BroadcastReceiver: Develop a class that extends BroadcastReceiver to listen for incoming SMS events.<\/li>\n<\/ol>\n<pre class=\"post-pre\"><code><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title class_\">SmsReceiver<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title class_\">BroadcastReceiver<\/span> {\r\n\r\n    <span class=\"hljs-meta\">@Override<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">void<\/span> <span class=\"hljs-title function_\">onReceive<\/span><span class=\"hljs-params\">(Context context, Intent intent)<\/span> {\r\n        <span class=\"hljs-keyword\">if<\/span> (intent.getAction().equals(<span class=\"hljs-string\">\"android.provider.Telephony.SMS_RECEIVED\"<\/span>)) {\r\n            <span class=\"hljs-type\">Bundle<\/span> <span class=\"hljs-variable\">bundle<\/span> <span class=\"hljs-operator\">=<\/span> intent.getExtras();\r\n            <span class=\"hljs-keyword\">if<\/span> (bundle != <span class=\"hljs-literal\">null<\/span>) {\r\n                Object[] pdus = (Object[]) bundle.get(<span class=\"hljs-string\">\"pdus\"<\/span>);\r\n                <span class=\"hljs-keyword\">if<\/span> (pdus != <span class=\"hljs-literal\">null<\/span>) {\r\n                    <span class=\"hljs-keyword\">for<\/span> (Object pdu : pdus) {\r\n                        <span class=\"hljs-type\">SmsMessage<\/span> <span class=\"hljs-variable\">smsMessage<\/span> <span class=\"hljs-operator\">=<\/span> SmsMessage.createFromPdu((<span class=\"hljs-type\">byte<\/span>[]) pdu);\r\n                        <span class=\"hljs-type\">String<\/span> <span class=\"hljs-variable\">messageBody<\/span> <span class=\"hljs-operator\">=<\/span> smsMessage.getMessageBody();\r\n                        <span class=\"hljs-comment\">\/\/ \u5728\u8fd9\u91cc\u5904\u7406\u77ed\u4fe1\u5185\u5bb9\uff0c\u63d0\u53d6\u9a8c\u8bc1\u7801\u5e76\u586b\u5165\u76f8\u5e94\u7684\u5b57\u6bb5<\/span>\r\n                    }\r\n                }\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/code><\/pre>\n<ol>\n<li>Register the BroadcastReceiver: Register the BroadcastReceiver in the onCreate method of the Activity where you need to read the verification code.<\/li>\n<\/ol>\n<pre class=\"post-pre\"><code><span class=\"hljs-type\">IntentFilter<\/span> <span class=\"hljs-variable\">intentFilter<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">IntentFilter<\/span>(<span class=\"hljs-string\">\"android.provider.Telephony.SMS_RECEIVED\"<\/span>);\r\n<span class=\"hljs-type\">SmsReceiver<\/span> <span class=\"hljs-variable\">smsReceiver<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">SmsReceiver<\/span>();\r\nregisterReceiver(smsReceiver, intentFilter);\r\n<\/code><\/pre>\n<ol>\n<li>Extract the verification code and fill in the field: In the onReceive method of SmsReceiver, you can extract the verification code using regular expressions or other methods, and then fill it into the corresponding field.<\/li>\n<\/ol>\n<p>For example, assuming the format of the verification code is &#8220;[App Name] Code: 123456, do not disclose to others&#8221;, you can use regular expressions to extract the code.<\/p>\n<pre class=\"post-pre\"><code><span class=\"hljs-type\">Pattern<\/span> <span class=\"hljs-variable\">pattern<\/span> <span class=\"hljs-operator\">=<\/span> Pattern.compile(<span class=\"hljs-string\">\"\u9a8c\u8bc1\u7801\uff1a(\\\\d+)\"<\/span>);\r\n<span class=\"hljs-type\">Matcher<\/span> <span class=\"hljs-variable\">matcher<\/span> <span class=\"hljs-operator\">=<\/span> pattern.matcher(messageBody);\r\n<span class=\"hljs-keyword\">if<\/span> (matcher.find()) {\r\n    <span class=\"hljs-type\">String<\/span> <span class=\"hljs-variable\">verificationCode<\/span> <span class=\"hljs-operator\">=<\/span> matcher.group(<span class=\"hljs-number\">1<\/span>);\r\n    <span class=\"hljs-comment\">\/\/ \u5c06\u9a8c\u8bc1\u7801\u586b\u5165\u76f8\u5e94\u7684\u5b57\u6bb5<\/span>\r\n}\r\n<\/code><\/pre>\n<ol>\n<li>Due to restrictions in Android versions 4.4 and above, it is not possible to directly access the contents of other apps&#8217; messages. If you need to read the verification codes from other apps&#8217; messages, users will need to manually authorize it or set the app as the default messaging app.<\/li>\n<li>Reading the SMS verification code requires user authorization and can be authorized through runtime permissions.<\/li>\n<\/ol>\n<p>These are the basic steps for automatically reading SMS verification codes in an Android application and filling them into the corresponding fields. Depending on the specific application scenario and requirements, some adaptive adjustments and optimizations may also be needed.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To automatically read SMS verification codes in an Android app and fill in the corresponding fields, you can follow these steps: Add permission: Include the permission to read SMS in the AndroidManifest.xml file. &lt;uses-permission android:name=&#8221;android.permission.READ_SMS&#8221; \/&gt; Create a BroadcastReceiver: Develop a class that extends BroadcastReceiver to listen for incoming SMS events. public class SmsReceiver extends [&hellip;]<\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-26878","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Android automatically reads the SMS verification code and fills it in. - Blog - Silicon Cloud<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android automatically reads the SMS verification code and fills it in.\" \/>\n<meta property=\"og:description\" content=\"To automatically read SMS verification codes in an Android app and fill in the corresponding fields, you can follow these steps: Add permission: Include the permission to read SMS in the AndroidManifest.xml file. &lt;uses-permission android:name=&quot;android.permission.READ_SMS&quot; \/&gt; Create a BroadcastReceiver: Develop a class that extends BroadcastReceiver to listen for incoming SMS events. public class SmsReceiver extends [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-16T07:31:14+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-03-22T09:39:48+00:00\" \/>\n<meta name=\"author\" content=\"Noah Thompson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Noah Thompson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/\"},\"author\":{\"name\":\"Noah Thompson\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a\"},\"headline\":\"Android automatically reads the SMS verification code and fills it in.\",\"datePublished\":\"2024-03-16T07:31:14+00:00\",\"dateModified\":\"2024-03-22T09:39:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/\"},\"wordCount\":246,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/\",\"name\":\"Android automatically reads the SMS verification code and fills it in. - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-16T07:31:14+00:00\",\"dateModified\":\"2024-03-22T09:39:48+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android automatically reads the SMS verification code and fills it in.\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a\",\"name\":\"Noah Thompson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g\",\"caption\":\"Noah Thompson\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/noahthompson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Android automatically reads the SMS verification code and fills it in. - Blog - Silicon Cloud","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/","og_locale":"en_US","og_type":"article","og_title":"Android automatically reads the SMS verification code and fills it in.","og_description":"To automatically read SMS verification codes in an Android app and fill in the corresponding fields, you can follow these steps: Add permission: Include the permission to read SMS in the AndroidManifest.xml file. &lt;uses-permission android:name=\"android.permission.READ_SMS\" \/&gt; Create a BroadcastReceiver: Develop a class that extends BroadcastReceiver to listen for incoming SMS events. public class SmsReceiver extends [&hellip;]","og_url":"https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-16T07:31:14+00:00","article_modified_time":"2024-03-22T09:39:48+00:00","author":"Noah Thompson","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Noah Thompson","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/"},"author":{"name":"Noah Thompson","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a"},"headline":"Android automatically reads the SMS verification code and fills it in.","datePublished":"2024-03-16T07:31:14+00:00","dateModified":"2024-03-22T09:39:48+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/"},"wordCount":246,"commentCount":0,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/","url":"https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/","name":"Android automatically reads the SMS verification code and fills it in. - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-16T07:31:14+00:00","dateModified":"2024-03-22T09:39:48+00:00","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/android-automatically-reads-the-sms-verification-code-and-fills-it-in\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Android automatically reads the SMS verification code and fills it in."}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/2e83cc6ab9f60d36921c2d0f9f280f4a","name":"Noah Thompson","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/350e537e1530ede2762ee0237e877d6693f4f7163ab4f303202cc9a6b27b6cb4?s=96&d=mm&r=g","caption":"Noah Thompson"},"url":"https:\/\/www.silicloud.com\/blog\/author\/noahthompson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/26878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=26878"}],"version-history":[{"count":1,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/26878\/revisions"}],"predecessor-version":[{"id":61072,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/26878\/revisions\/61072"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=26878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=26878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=26878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}