Android API: SMS service and Contacts

Source: Internet
Author: User
Android API supports the development of applications that can send and receive SMS messages. Program . Currently, the android simulator we use in the development process does not support sending SMS, but it can receive SMS. Now let's explore Android's support for SMS. We will build a small application to listen to the SMS messages received on mobile devices (or simulators) and display them. Let's define an intent receiver to handle the SMS Receiving Event: Package COM. wissen. SMS. extends er; public class smsreceiver extends broadcastreceiver {@ override public void onreceive (context, intent) {// todo} package COM. wissen. SMS. extends er; public class smsreceiver extends broadcastreceiver {@ overridepublic void onreceive (context, intent) {// todo} We need to configure this intent receiver so that it can get the SMS Receiving Event, 'android. provide R. telephony. sms_received event status indicates that the SMS has been received. We can go to androidmanifest. configure the following in XML: <er Android: Name = ". aggreger. smsreceiver "Android: enabled =" true "> <intent-filter> <action Android: Name =" android. provider. telephony. sms_received "/> </intent-filter> </Cycler> <Cycler Android: Name = ". aggreger. smsreceiver "Android: enabled =" true "> <intent-filter> <action Android: Name =" android. provider. telephony. sms_received "/> </intent-filter> </Cycler> to enable our application To receive SMS messages, you must specify the permissions first. <uses-Permission Android: Name = "android. permission. receive_sms "> </uses-Permission> <uses-Permission Android: Name =" android. permission. receive_sms "> </uses-Permission> now, our intent receiver can be called when the Android device receives SMS, the rest is to get and display the received SMS message text: Public void onreceive (context, intent) {bundle = intent. getextras (); object messages [] = (object []) Bundle. get ("PDUS"); smsmessage [] = new smsmessage [messages. length]; for (INT n = 0; N & lt; messages. length; n ++) {smsmessage [N] = smsmessage. createfrompdu (byte []) messages [N]);} // show first message toast = toast. maketext (context, "received SMS:" + smsmessage [0]. getmessagebody (), toast. length_long); toast. show ();} public void onreceive (context, intent) {bun Dle bundle = intent. getextras (); object messages [] = (object []) bundle. get ("PDUS"); smsmessage [] = new smsmessage [messages. length]; for (INT n = 0; N & lt; messages. length; n ++) {smsmessage [N] = smsmessage. createfrompdu (byte []) messages [N]);} // show first messagetoast toast = toast. maketext (context, "received SMS:" + smsmessage [0]. getmessagebody (), toast. length_long); toast. show ();} android The SMS received by the device is in the form of a PDU (protocol description unit ). Android. telephony. GSM. the smsmessage class can store SMS-related information. We can also create a smsmessage instance from the received PDU, the toast interface component displays the received SMS message text in the form of system notifications. Run the program: Now let's run the application in the simulator and send the SMS message to the simulator. In the ddms view (Dalvik debug monitor service) provided by the android plug-in of Eclipse, we can send SMS messages to the simulator (in the 'emulator control' panel; in addition, you need to specify the phone number, but it can be any one.) public static final string music_action = "com. mythlink. music "; intent = new intent (); intent. setaction (music_action); intent. putextra ("music_path", songpath); this. sendbroadcast (intent); public static final string music_action = "com. mythlink. music "; intent = new intent (); intent. setaction (music_action); intent. putextra ("music_path", songpath); this. sendbroadcast (intent); you need to write another broadcast receiver public class musicreceiver extends broadcastreceiver {@ override public void onreceive (context, intent) {bundle = intent. getextras (); string music_path = bundle. getstring ("music_path"); Toast = toast. maketext (context, "playing music:" + music_path, toast. length_long); toast. show () ;}} public class musicreceiver extends broadcastreceiver {@ overridepublic void onreceive (context, intent) {bundle = intent. getextras (); string music_path = bundle. getstring ("music_path"); Toast = toast. maketext (context, "playing music:" + music_path, toast. length_long); toast. show () ;}} get the contact information public class contactslist extends listactivity {private listadapter madapter; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); cursor c = This. getcontentresolver (). query (contacts. people. content_uri, null, null); this. startmanagingcursor (c); string [] Columns = new string [] {contacts. people. name}; int [] names = new int [] {R. id. SONG}; // madapter = new simplecursoradapter (this, R. layout. song_item, C, columns, names); this. setlistadapter (madapter) ;}@ override protected void onlistitemclick (listview L, view V, int position, long ID) {super. onlistitemclick (L, V, position, ID); intent I = new intent (intent. action_call); cursor c = (cursor) madapter. getitem (position); long phoneid = C. getlong (C. getcolumnindex (contacts. people. primary_phone_id); I. setdata (contenturis. withappendedid (contacts. phones. content_uri, phoneid); this. startactivity (I) ;}} public class contactslist extends listactivity {private listadapter madapter; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); cursor c = This. getcontentresolver (). query (contacts. people. content_uri, null, null); this. startmanagingcursor (c); string [] Columns = new string [] {contacts. people. name}; int [] names = new int [] {R. id. SONG}; // madapter = new simplecursoradapter (this, R. layout. song_item, C, columns, names); this. setlistadapter (madapter) ;}@ overrideprotected void onlistitemclick (listview L, view V, int position, long ID) {super. onlistitemclick (L, V, position, ID); intent I = new intent (intent. action_call); cursor c = (cursor) madapter. getitem (position); long phoneid = C. getlong (C. getcolumnindex (contacts. people. primary_phone_id); I. setdata (contenturis. withappendedid (contacts. phones. content_uri, phoneid); this. startactivity (I) ;}} in androidmanifest. add <uses-Permission Android: Name = "android. permission. read_contacts "/> <activity Android: Name = ". contactslist "Android: Label =" @ string/app_name "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter> </activity>
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.