公司内部有过需求 需要插入通过自己app插入短信
刚开始觉得挺简单的,就是往数据库加数据而已,
插入短信
ContentResolver resolver=getContentResolver();
Uri url=Uri.parse("content://sms/");
ContentValues values=new ContentValues();
values.put("address", "10086");
values.put("type", 1);
values.put("date", System.currentTimeMillis());
values.put("body", "我是10086的,快开门");
resolver.insert(url, values);
执行了代码发现短信里没有。。。。 最后发现不是这么简单,
Android系统从4.4以后就除去默认短信以外其他应用无法插入短信,好吧我们就把我们的app做成默认短信
默认短息的条件
<!-- 具备短信默认应用条件 ① -->
<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>
<!-- 具备短信默认应用条件 ②-->
<!-- 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>
<!-- 具备短信默认应用条件 ④ -->
<!-- Service that delivers messages from the phone "quick response" -->
<service
android:name=".HeadlessSmsSendService"
android:exported="true"
android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE">
<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>
增加这些条件之后 再应用设置里就可以设置为默认短信了,但是需要去设置中设置也很麻烦。