从4.4 之后系统设置里面允许用户修改默认的短信应用,只有被选中的短信应用才可以对SMSProvider执行写入操作。在收到信息时 该默认的SMS信息时应用会收到SMS_DELIVER_ACTION 广播,在收到MMS信息时,会收到WAP_PUSH_DELIVER_ACTION广播。 在接收到或者发送短信的时候,改默认应用负责讲消息内容写入到SMS Provider.
其他没有被选作默认消息处理的应用只能读取SMSProvider. 但是在接收到新消息的时候还是可以监听到 SMS_RECEIVED_ACTION 广播,这样做的目的是以便其他的应用可以在接收到消息之后,做其他的读取操作。
为了让你的应用出现在系统设置中的默认消息处理应用界面,你需要在manifest中做以下声明,
在一个broadcast receiver中,加入intent-filter action SMS_DELIVER_ACTION ("android.provider.Telephony.SMS_DELIVER" ) 这个receiver需要BROADCAST_SMS权限。
在另一个broadcast receiver中声明intent-filter WAP_PUSH_DELIVER_ACTION ("android.provider.Telephony.WAP_PUSH_DELIVER" ) 并且声明data 的mime类型为 "application/vnd.wap.mms-message"。 这个receiver需要BROADCAST_WAP_PUSH权限。 以上的操作就可以直接的接收到mms消息。
在发送消息的activity中,加入intent-filter ACTION_SENDTO ("android.intent.action.SENDTO" )
并且声明 data schema sms , smsto , mms , 和 mmsto。 这个声明可以让你的应用接收到其他想要发送短信的应用所发送的intent.
定义一个service,加入intent-filter ACTION_RESPONSE_VIA_MESSAGE ("android.intent.action.RESPOND_VIA_MESSAGE" ) 并且声明 data schema sms , smsto , mms , 和 mmsto。 他允许用户使用你的应用以文本的形式对一个来电作出相应。
下面是manifest 文件的例子, 所有内容都是必须的。 <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>
对于一些其他的并不想作为默认的sms 应用,但是仍然想要发送短信的应用, 其他任何的包含了 SEND_SMS 权限的应用,还是可以使用SmsManager 来发送SMS消息。
只有在android 4.4 系统并且没有被设置为默认的SMS应用的时候,系统会自动的将消息写入SMSprovider. (通常情况下由默认的SMS应用负责写入)。
在你的应用需要执行默认SMS应用的操作,但是还没有被选作默认的SMS应用时,比如删除短信,写入短信,编写草稿,标记为已读等操作。
在Activity resume的时候检查你的应用是不是默认的SMS应用 Telephony.Sms.getDefaultSmsPackage()
这个方法返回默认的SMS 应用的包名。 如果和你应用报名不同,则可以通过代码Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT); intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, myPackageName); startActivity(intent); 来要求切换默认的SMS应用。 他会弹出一个dialog,是否要将该应用设置为默认的SMS应用。
示例代码: 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); } } } 在你使用完功能之后需要将默认的SMS 应用还原以保证用户原来的默认SMS 应用可以正常处理数据。
通常流程如下: 1. 保存当前的默认SMS 应用的包名。
String defaultSmsApp = Telephony.Sms.getDefaultSmsPackage(context); 2. 请求用户切换默认SMS应用为你的应用, Intent intent = new Intent(context, Sms.Intents.ACTION_CHANGE_DEFAULT); intent.putExtra(Sms.Intents.EXTRA_PACKAGE_NAME, context.getPackageName()); startActivity(intent); 3. 在执行完操作之后,请求用户修改默认SMS应用为原来的应用。 Intent intent = new Intent(context, Sms.Intents.ACTION_CHANGE_DEFAULT); intent.putExtra(Sms.Intents.EXTRA_PACKAGE_NAME, defaultSmsApp); startActivity(intent);
分享源码 百度云盘:
http://pan.baidu.com/s/1jGxZQHw
|