[Android article]android 4.4 SMS adaptation

Source: Internet
Author: User

Andorid 4.4 (KITKAT) has made changes to many aspects of the system, in the current project, the text has a direct impact. Let's take a look at the following document description:

Advice for SMS backup & Restore Apps

Because the ability to write to the Smsprovider are restricted to the app the user selects as the default SMS app, Anyexist ing app designed purely to backup and restore SMS messages would currentlybe unable to restore SMS messages on Android 4.4. An app, backs up andrestores SMS messages must also is set as the default SMS app so that it canwrite messages in the SMS Provider.


Therefore, we need to do a corresponding adaptation of 4.4

    • In a broadcast receiver, include an intent filter for SMS_DELIVER_ACTION ( "android.provider.Telephony.SMS_DELIVER" ). The broadcast receiver must also require the BROADCAST_SMS permission.

      This allows your app to directly receive incoming SMS messages.

public class Smsreceiver extends Broadcastreceiver {@Override public void onreceive (context context, Intent Intent) {        Bundle extras = Intent.getextras ();        if (extras = = null) {return;        } object[] Smsextras = (object[]) extras.get ("PDUs");        if (Smsextras = = NULL | | smsextras.length = = 0) {return; } contentresolver Contentresolver = Context.getcontentresolver (); for (Object Smsextra:smsextras) {try {byte[] smsbytes = (byte[]) Smsextra; Smsmessage smsmessage = SMSMESSAGE.CREATEFROMPDU (smsbytes); Contentvalues values = new Contentvalues (); Values.put (Sms.address, smsmessage.getoriginatingaddress ()); Values.put ( Sms.body, Smsmessage.getmessagebody ()); Values.put (Sms.date, Smsmessage.gettimestampmillis ()); Values.put (Sms.READ , 0); Values.put (Sms.type, Sms.message_type_inbox); Contentresolver.insert (Uri.parse ("content://sms"), values);}    catch (Exception e) {e.printstacktrace ();}} }}

    • In a broadcast receiver, include an intent filter for WAP_PUSH_DELIVER_ACTION ( "android.provider.Telephony.WAP_PUSH_DELIVER" ) with the MIME type "application/vnd.wap.mms-message" . The broadcast receiver must also require the BROADCAST_WAP_PUSH permission.

      This allows your app to directly receive incoming MMS messages.

public class Mmsreceiver extends Broadcastreceiver {@Overridepublic void OnReceive (context context, Intent Intent) {//TOD O}}
MMS does not use much in daily life, does not do the corresponding treatment

    • In your activity this delivers new messages, include an intent filter for ACTION_SENDTO ( "android.intent.action.SENDTO" ) with schemas, sms: , smsto: , mms: , and mmsto: .

      This allows your app-to-receive intents from the other apps, the want to deliver a message.

public class Composesmsactivity extends Activity {}

    • In a service, include an intent filter for ACTION_RESPONSE_VIA_MESSAGE ( "android.intent.action.RESPOND_VIA_MESSAGE" ) with schemas,,,, and sms: smsto: mms: mmsto: . This service must also require the SEND_RESPOND_VIA_MESSAGE permission.

      This allows users and respond to incoming phone calls with an immediate text message using your app.

public class Headlesssmssendservice extends Intentservice {public headlesssmssendservice () {Super (headlesssmssends        Ervice.class.getName ());    Setintentredelivery (TRUE);        } @Override protected void Onhandleintent (Intent Intent) {String action = intent.getaction (); if (!        TelephonyManager.ACTION_RESPOND_VIA_MESSAGE.equals (ACTION)) {return;        } Bundle Extras = Intent.getextras ();        if (extras = = null) {return;        } String message = Extras.getstring (Intent.extra_text);        Uri Intenturi = Intent.getdata ();        String recipients = getrecipients (Intenturi);        if (textutils.isempty (recipients)) {return;        } if (Textutils.isempty (message)) {return;        } string[] Destinations = textutils.split (recipients, ";");    Sendandstoretextmessage (Getcontentresolver (), destinations, message); }/** * Get quick response recipients from URI */PRivate string getrecipients (Uri uri) {string base = Uri.getschemespecificpart ();        int pos = Base.indexof ('? '); return (pos = =-1)?    Base:base.substring (0, POS); }/** * Send text message to recipients and store the message to SMS Content Provider * * @param contentres Olver Contentresolver * @param destinations recipients of message * @param message message */private void Sendandstoretextmessage (Contentresolver contentresolver, string[] destinations, String message) {Smsmanager SmsMan        Ager = Smsmanager.getdefault (); for (String destination:destinations) {try{smsmanager.sendtextmessage (destination, NULL, message, NU            ll, NULL);            Contentvalues values = new Contentvalues ();            Values.put (sms.address, destination); Values.put (sms.body, message); Values.put (Sms.date, System.currenttimemillis ()); Values.put (sms.read, 0); values.put            (Sms.type, sms.message_type_sent); COntentresolver.insert (Uri.parse ("content://sms"), values);        }catch (Exception e) {e.printstacktrace (); }        }    }}

Androidmenifest.xml:

 <!--Activity that allows the user to send new SMS/MMS messages---<activity android:name= "Packagename.a Ctivity. Composesmsactivity "> <intent-filter> <action android:name=" Android.intent.action.SE ND "/> <action android:name=" Android.intent.action.SENDTO "/> <c Ategory android:name= "Android.intent.category.DEFAULT"/> <category android:name= "android.intent.cate Gory.                browsable "/> <data android:scheme=" sms "/> <data android:scheme=" Smsto "/> <data android:scheme= "MMS"/> <data android:scheme= "Mmsto"/> </int Ent-filter> </activity> <!--broadcastreceiver that listens for incoming SMS messages-- > <receiver android:name= "packageName.receiver.SmsReceiver" android:permission= "Android.permis Sion. BroadcAst_sms "> <intent-filter> <action android:name=" Android.provider.Telephony.SMS_DELIV ER "/> </intent-filter> </receiver> <!--Broadcastreceiver that listens for Inc Oming MMS messages-<receiver android:name= "PackageName.receiver.MmsReceiver" Android:permissio n= "Android.permission.BROADCAST_WAP_PUSH" > <intent-filter> <action android:name= "and  Roid.provider.Telephony.WAP_PUSH_DELIVER "/> <data android:mimetype=" Application/vnd.wap.mms-message " /> </intent-filter> </receiver> <!--Service that delivers messages fr                 Om the phone "quick response"-<service android:name= "PackageName.service.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"/> &lt ;d ata android:scheme= "Smsto"/> <data android:scheme= "mms"/> <data android:sche Me= "Mmsto"/> </intent-filter> </service>

Another: <manifest/> Inside Don't forget add android:label= "@string/app_name"


For English documents, please visit: http://android-developers.blogspot.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.