Unable to Read SMS Android application code

1 week ago 13
ARTICLE AD BOX

I am creating a Smart SMS Application, which will replace the default Android "Message" applicaiton used to send and receive message, I am able to send messages but unable to read it.

I read somewhere now a days its not possible to read application until you make your app as "default sms app"

but in order to do that we have to fulfill certain functionalities. Which i think I have completed

Add required permissions Add receivers like BROADCAST_SMS and BROADCAST_WAP_PUSH Main application to implement intent-filters Request permission on runtime (user click action)

but nothing work at all, can someone please guide me how to do it. I do see some popups on application start (and i allow them), but not able to read messages, as it never goes forward when i click on "Inbox" button

Settings -> Apps -> Default Apps -> SMS App (Messages) Can not see my application in default Apps

Inbox Button click

onNavigateToInbox = { if (Telephony.Sms.getDefaultSmsPackage(this@MainActivity) != packageName) { requestDefaultSmsApp() } else { navController.navigate("inbox") } }

Permissions

xmlns:tools="http://schemas.android.com/tools"> <!-- REQUIRED: telephony capability --> <uses-feature android:name="android.hardware.telephony" android:required="true" /> <!-- SMS permissions --> <uses-permission android:name="android.permission.SEND_SMS" /> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.READ_SMS" /> <uses-permission android:name="android.permission.WRITE_SMS" /> <uses-permission android:name="android.permission.RECEIVE_WAP_PUSH" />

Intent filters

and here is I added in application tag as well

android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.Xter"> <!-- MAIN / SMS HANDLING ACTIVITY --> <activity android:name=".MainActivity" android:exported="true" android:label="@string/app_name" android:theme="@style/Theme.Xter"> <!-- Launcher --> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <!-- REQUIRED: send SMS entry point --> <intent-filter> <action android:name="android.intent.action.SENDTO" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="sms" /> <data android:scheme="smsto" /> </intent-filter> </activity>

Receivers

I have also added the receivers

<receiver android:name=".sms.SmsReceiver" android:permission="android.permission.BROADCAST_SMS" android:exported="true"> <intent-filter> <action android:name="android.provider.Telephony.SMS_DELIVER" /> </intent-filter> </receiver> <!-- REQUIRED: MMS receiver (even if unused) --> <receiver android:name=".sms.MmsReceiver" android:permission="android.permission.BROADCAST_WAP_PUSH" android:exported="true"> <intent-filter> <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" /> <data android:mimeType="application/vnd.wap.mms-message" /> </intent-filter> </receiver>

Request Permissions

Added following in MainActivity file onCreate method

override fun onCreate(savedInstanceState: Bundle?) { .... setContent { XterTheme { LaunchedEffect(Unit) { if (Telephony.Sms.getDefaultSmsPackage(this@MainActivity) != packageName) { requestDefaultSmsApp() } } .... }

here is how my requestDefaultSmsApp function look like

private fun requestDefaultSmsApp() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { val roleManager = getSystemService(RoleManager::class.java) if (roleManager.isRoleAvailable(RoleManager.ROLE_SMS) && !roleManager.isRoleHeld(RoleManager.ROLE_SMS) ) { val intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_SMS) startActivity(intent) } } else { val intent = Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT) intent.putExtra( Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, packageName ) startActivity(intent) } }

but nothing work

Read Entire Article