SMS Read received SMS Read sent SMS Send
- Slides: 6
SMS • • • Read received SMS Read sent SMS Send SMS New app, SMSFun Button – Text: Send SMS – Id: send. SMSButton • Permissions – – send_sms read_sms receive_sms // not broadcast_sms. This is for when the system announces a SMS has been received. But only the system is allowed to make use of this permission. There a few permissions like this.
• overview – – • Reading a received SMS Make SMSReceiver class that extends Broadcast. Receiver Use manifest to tell system to send received SMS to this class New class – – SMSReceiver extends Broadcast. Receiver In on Receive, add • • • Bundle bundle = intent. get. Extras(); Sms. Message[] msgs = null; if (bundle != null) { – – • • } Object[] pdus = (Object[]) bundle. get("pdus"); msgs = new Sms. Message[pdus. length]; for (int i=0; i<msgs. length; i++) { » msgs[i] = Sms. Message. create. From. Pdu((byte[])pdus[i]); » String str = ""; » str += "SMS from " + msgs[i]. get. Originating. Address() + " "; » str += "SMS Body "+ msgs[i]. get. Message. Body(). to. String() + " "; » //---display the new SMS message--» Toast. make. Text(context, str, Toast. LENGTH_SHORT). show(); » Log. e("SMS Receiver", "Received Message: "+str); } In manifest – Application tab • Application nodes – Add » Receiver • • In application nodes – – • Click on the SMSReciever that was just added Add » Intent. Filter In application nodes – – • On right, in name, browse and find the class you just made Click on Intent. Filter just added Add » Action • On right, select android. provider. Telephony. SMS_RECEIVED Run: send a sms to this phone and see if it shows up
Recording sent SMS • New class SMSRecorder • Member variables – – Context context; Handler handler; Content. Resolver content. Resolver; public My. Observer my. Observer; • Constructor – SMSRecorder(Context _context) { – } • this. context = _context; • handler = new Handler(); • public void start. Recording() { – my. Observer = new My. Observer(handler); – content. Resolver = context. get. Content. Resolver(); – content. Resolver. register. Content. Observer(Uri. parse("content: //sms"), true, my. Observer); • } • public void stop. Recording() { • } – content. Resolver. unregister. Content. Observer(my. Observer);
Content. Observer • class My. Observer extends Content. Observer { – public My. Observer(Handler handler) { • super(handler); – } – @Override – public void on. Change(boolean self. Change) { • • • super. on. Change(self. Change); Uri uri. SMSURI = Uri. parse("content: //sms"); Cursor cur = context. get. Content. Resolver(). query(uri. SMSURI, null, null); // this will make it point to the first record, which is the last SMS sent cur. move. To. Next(); • String content = cur. get. String(cur. get. Column. Index("body")); • String address = "nothing"; //cur. get. String(cur. get. Column. Index("address")); • Log. e("sms recorder", "sms sent to: "+address+" with body: "+content); • // show each column. . . each piece of data that could be recorded • for (int i=0; i<cur. get. Column. Count(); i++) – Log. e("sms recorder: column", cur. get. Column. Name(i)); • } – } • cur. close(); // or call cur. move. To. Next(); to process an older sms
In Activity • Member variable – SMSRecorder sms. Recorder; • In on. Create – sms. Recorder = new SMSRecorder(this); – sms. Recorder. start. Recording(); • New function – @Override – public void on. Destroy() { – } • super. on. Destroy(); • sms. Recorder. stop. Recording(); • Run: send a sms somewhere
Send SMS • In on. Create – Button send. SMSButton = (Button)find. View. By. Id(R. id. send. SMSButton); – send. SMSButton. set. On. Click. Listener(new View. On. Click. Listener() { • @Override • public void on. Click(View v) { Pending. Intent pi = Pending. Intent. get. Activity(SMSFun. Activity. this, 0, new Intent(SMSFun. Activity. this, SMSFun. Activity. class), 0); Sms. Manager sms = Sms. Manager. get. Default(); sms. send. Text. Message("1234567890", null, "test", pi, null); // first null is the SMS center which will receive and route the SMS. The second null could be an intent that will be generated when the sms is sent – //sms. send. Multipart. Text. Message – //sms. send. Data. Message – – • } – }); • Run. Try you own number (i. e. , send sms to yourself)