官方文档说明:
Telephony的Content Provider("SMS Provider")允许设备上的应用去读写短信和彩信。SMS Provider包括了接收,发送信息的表。
从Android4.4开始,系统设置允许用户设置一个“默认短信应用”.设置后,只有默认短信应用能够有权限去写"SMS Provider",但用户接到到短信或彩信时,只有默认短信应用能够接到到SMS_DELIVER_ACTION广播或WAP_PUSH_DELIVER_ACTION广播。同样,默认短信应用负责把短信的详细信息写入SMS Provider。
其他没有被设置为默认短信应用的应用只能够读取SMS Provider,但这些应用也许会在设备接收到短信时收到SMS_RECEIVED_ACTION广播,因为SMS_RECEIVED_ACTION广播是不可中止的,所以可能会发送给多个应用。这个广播就是为那些没有被设置为默认短信应用的应用在需要读取接收到短信内容准备的,例如在做手机号码验证时。
从Android4.4开始,系统设置允许用户设置一个“默认短信应用”.设置后,只有默认短信应用能够有权限去写"SMS Provider",但用户接到到短信或彩信时,只有默认短信应用能够接到到SMS_DELIVER_ACTION广播或WAP_PUSH_DELIVER_ACTION广播。同样,默认短信应用负责把短信的详细信息写入SMS Provider。
其他没有被设置为默认短信应用的应用只能够读取SMS Provider,但这些应用也许会在设备接收到短信时收到SMS_RECEIVED_ACTION广播,因为SMS_RECEIVED_ACTION广播是不可中止的,所以可能会发送给多个应用。这个广播就是为那些没有被设置为默认短信应用的应用在需要读取接收到短信内容准备的,例如在做手机号码验证时。
如何使你的APP在默认短信设置中出现
为了让你的应用作为“默认短信应用”在系统设置中出现,你的Manifest文件必须做一些必要的声明,包括以下几个方面:
1.必须包含一个接收 SMS_DELIVER_ACTION ("android.provider.Telephony.SMS_DELIVER")广播的receiver。这个广播接收器也必须声明BROADCAST_SMS 权限。
~上面是为了让你的应用直接介绍将要到来的短信
2.必须包含一个接收WAP_PUSH_DELIVER_ACTION ("android.provider.Telephony.WAP_PUSH_DELIVER") 广播的receiver,且其MIME类型为"application/vnd.wap.mms-message"。这个广播接收器额必须声明BROADCAST_WAP_PUSH 权限。
~上面是为了允许你的应用直接接受将要到来的彩信。
3.在你要发送信息的Activity中,需要添加包含sms:,smsto:,mms:,mmsto:shemas的ACTION_SENDTO ("android.intent.action.SENDTO")intent filter。
~上面允许你接受其他想要发送短信的应用发出的intent。
4.必须声明一个service,需要添加包含sms:,smsto:,mms:,mmsto:shemas的ACTION_RESPONSE_VIA_MESSAGE ("android.intent.action.RESPOND_VIA_MESSAGE")intent filter。并且这个service需要声明 SEND_RESPOND_VIA_MESSAGE权限。
~上面是为了让你的应用直接介绍将要到来的短信
2.必须包含一个接收WAP_PUSH_DELIVER_ACTION ("android.provider.Telephony.WAP_PUSH_DELIVER") 广播的receiver,且其MIME类型为"application/vnd.wap.mms-message"。这个广播接收器额必须声明BROADCAST_WAP_PUSH 权限。
~上面是为了允许你的应用直接接受将要到来的彩信。
3.在你要发送信息的Activity中,需要添加包含sms:,smsto:,mms:,mmsto:shemas的ACTION_SENDTO ("android.intent.action.SENDTO")intent filter。
~上面允许你接受其他想要发送短信的应用发出的intent。
4.必须声明一个service,需要添加包含sms:,smsto:,mms:,mmsto:shemas的ACTION_RESPONSE_VIA_MESSAGE ("android.intent.action.RESPOND_VIA_MESSAGE")intent filter。并且这个service需要声明 SEND_RESPOND_VIA_MESSAGE权限。
完整示例代码
<manifest> ... <application> <!-- BroadcastReceiver that listens for incoming SMS messages --> <receiver android:name=".SmsReceiver" android:permission="android.permission.BROADCAST_SMS"> <intent-filter> <action android:name="android.provider.Telephony.SMS_DELIVER" /> </intent-filter> </receiver> <!-- BroadcastReceiver that listens for incoming MMS messages --> <receiver android:name=".MmsReceiver" android:permission="android.permission.BROADCAST_WAP_PUSH"> <intent-filter> <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" /> <data android:mimeType="application/vnd.wap.mms-message" /> </intent-filter> </receiver> <!-- Activity that allows the user to send new SMS/MMS messages --> <activity android:name=".ComposeSmsActivity" > <intent-filter> <action android:name="android.intent.action.SEND" /> <action android:name="android.intent.action.SENDTO" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="sms" /> <data android:scheme="smsto" /> <data android:scheme="mms" /> <data android:scheme="mmsto" /> </intent-filter> </activity> <!-- Service that delivers messages from the phone "quick response" --> <service android:name=".HeadlessSmsSendService" android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE" android:exported="true" > <intent-filter> <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" /> <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> </service> </application> </manifest>
触发设置更改默认短信应用
1.通过Telephony.Sms.getDefaultSmsPackage()获取到当前的默认短信应用的包名
2.与本应用包名比较,是否相同。如相同就说明当前应用是默认应用,如果不同就说明当前应用不是默认短信应用。
3.发送更改默认短信应用Intent。
完整代码:
public class ComposeSmsActivity extends Activity { @Override protected void onResume() { super.onResume(); final String myPackageName = getPackageName(); if (!Telephony.Sms.getDefaultSmsPackage(this).equals(myPackageName)) { // App is not default. // Show the "not currently set as the default SMS app" interface View viewGroup = findViewById(R.id.not_default_app); viewGroup.setVisibility(View.VISIBLE); // Set up a button that allows the user to change the default SMS app Button button = (Button) findViewById(R.id.change_default_app); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT); intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, myPackageName); startActivity(intent); } }); } else { // App is the default. // Hide the "not currently set as the default SMS app" interface View viewGroup = findViewById(R.id.not_default_app); viewGroup.setVisibility(View.GONE); } } }
如果需要回复到以前的默认应用,你只需要把之前的默认应用包名保存下来,通过以上步骤重新设置即可。
参考:
https://developer.android.com/about/versions/android-4.4.html#SMS
http://android-developers.blogspot.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html