Skip to content

Commit 0aebba4

Browse files
fix(messages): Improve reliability of opening the default SMS app
This commit adds a fallback mechanism to the `openTextMessagesApp` function. Previously, the function relied solely on a generic `CATEGORY_APP_MESSAGING` intent, which could fail. Now, if the initial attempt fails, it will try to get the default SMS package using `Telephony.Sms.getDefaultSmsPackage(this)` and launch it directly. This increases the chances of successfully opening the user's chosen text messaging application.
1 parent 739c407 commit 0aebba4

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

app/src/main/java/com/github/codeworkscreativehub/common/ContextExtensions.kt

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import android.provider.AlarmClock
2222
import android.provider.CalendarContract
2323
import android.provider.MediaStore
2424
import android.provider.Settings
25+
import android.provider.Telephony
2526
import android.text.format.DateFormat
2627
import android.view.LayoutInflater
2728
import android.view.View
@@ -131,8 +132,18 @@ fun Context.openTextMessagesApp() {
131132
flags = Intent.FLAG_ACTIVITY_NEW_TASK
132133
}
133134
this.startActivity(intent)
134-
} catch (e: Exception) {
135-
AppLogger.d("openTextMessagesApp", e.toString())
135+
} catch (_: Exception) {
136+
try {
137+
val defaultSmsPackage = Telephony.Sms.getDefaultSmsPackage(this)
138+
139+
if (defaultSmsPackage != null) {
140+
val intent = packageManager.getLaunchIntentForPackage(defaultSmsPackage)
141+
intent?.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
142+
startActivity(intent)
143+
}
144+
} catch (e: Exception) {
145+
AppLogger.d("openTextMessagesApp", e.toString())
146+
}
136147
}
137148
CrashHandler.logUserAction("Text Messages App Launched")
138149
}

0 commit comments

Comments
 (0)