Android中如何在自定义短信应用内唤起默认短信应用选择器?
Hey there! Since you’ve already built a messaging app that can be set as the default, launching the system’s default SMS app picker directly from your app is totally doable—here’s how to pull it off for Android (iOS doesn’t have a default SMS app selection flow like this):
Step 1: Verify Required Permissions
First, make sure your app has the necessary permissions and manifest declarations to qualify as a default SMS app. Add these to your AndroidManifest.xml:
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" />
SMS Role Declaration (Android 10+)
Inside your main activity’s intent filter, declare support for SMS-related actions to let the system recognize your app as a valid default candidate:
<application ...> <activity ...> <intent-filter> <action android:name="android.intent.action.SEND" /> <action android:name="android.intent.action.SENDTO" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="sms" /> <data android:scheme="smsto" /> <data android:scheme="mms" /> <data android:scheme="mmsto" /> </intent-filter> </activity> </application>
Step 2: Trigger the Default App Picker
Use an explicit system intent to launch the default SMS app selection screen directly from your app. Here are implementations for both Kotlin and Java:
Kotlin Example
import android.provider.Telephony import android.content.Intent // Call this from your activity/fragment fun launchDefaultSmsPicker() { val intent = Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT) intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, packageName) startActivity(intent) }
Java Example
import android.provider.Telephony; import android.content.Intent; // Call this from your activity/fragment public void launchDefaultSmsPicker() { Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT); intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, getPackageName()); startActivity(intent); }
Key Tips for Better UX
- Check Current Default First: Avoid launching the picker unnecessarily by checking if your app is already the default. Use
Telephony.Sms.getDefaultSmsPackage(context)to get the current default package name, then compare it to your app’s package name. - Add a Clear Prompt: Before launching the picker, show a friendly message like "Set [Your App Name] as your default messaging app to send/receive texts seamlessly?" so users know what to expect.
- Compatibility: This intent works on Android 4.4 (API level 19) and above, which is when the default SMS app system was first introduced.
内容的提问来源于stack exchange,提问作者Antony Kavoo

