Android insert SMS function

There has been a need within the company to insert text messages through its own app

At first I thought it was quite simple, just adding data to the database,

Insert SMS

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);

Executed the code and found that there is no text message. . . . In the end, I found that it is not so simple.
Since Android system 4.4, other applications except the default text message cannot insert text messages. Well, let’s make our app the default text message.

The default short message conditions

<!-- 具备短信默认应用条件 ① -->
               <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>

After adding these conditions, it can be set as the default SMS in the application settings, but it is also very troublesome to set in the settings.

Guess you like

Origin blog.csdn.net/yanwenyuan0304/article/details/122258019