sms read received sms read sent sms send sms new app, smsfun button – text: send sms – id:...

6
SMS • Read received SMS • Read sent SMS • Send SMS • New app, SMSFun • Button – Text: Send SMS – Id: sendSMSButton • 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 are a few permissions like this.

Upload: giles-short

Post on 22-Dec-2015

220 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: SMS Read received SMS Read sent SMS Send SMS New app, SMSFun Button – Text: Send SMS – Id: sendSMSButton Permissions – send_sms – read_sms – receive_sms

SMS• Read received SMS• Read sent SMS• Send SMS• New app, SMSFun• Button

– Text: Send SMS– Id: sendSMSButton

• 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 are a few permissions like this.

Page 2: SMS Read received SMS Read sent SMS Send SMS New app, SMSFun Button – Text: Send SMS – Id: sendSMSButton Permissions – send_sms – read_sms – receive_sms

Reading a received SMS• overview

– Make SMSReceiver class that extends BroadcastReceiver– Use manifest to tell system to send received SMS to this class

• New class– SMSReceiver extends BroadcastReceiver– In on Receive, add

• Bundle bundle = intent.getExtras(); • SmsMessage[] msgs = null;• if (bundle != null) {

– Object[] pdus = (Object[]) bundle.get("pdus");– msgs = new SmsMessage[pdus.length]; – for (int i=0; i<msgs.length; i++) {

» msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);» String str = "";» str += "SMS from " + msgs[i].getOriginatingAddress() + " "; » str += "SMS Body "+ msgs[i].getMessageBody().toString() + " ";» //---display the new SMS message---» Toast.makeText(context, str, Toast.LENGTH_SHORT).show();» Log.e("SMS Receiver","Received Message: "+str);

– }

• }

• In manifest– Application tab

• Application nodes– Add

» Receiver• On right, in name, browse and find the class you just made

• In application nodes– Click on the SMSReciever that was just added– Add

» IntentFilter

• In application nodes– Click on IntentFilter 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

Page 3: SMS Read received SMS Read sent SMS Send SMS New app, SMSFun Button – Text: Send SMS – Id: sendSMSButton Permissions – send_sms – read_sms – receive_sms

Recording sent SMS

• New class SMSRecorder• Member variables

– Context context;– Handler handler;– ContentResolver contentResolver;– public MyObserver myObserver;

• Constructor– SMSRecorder(Context _context) {

• this.context = _context;• handler = new Handler();

– }• public void startRecording() {

– myObserver = new MyObserver(handler);– contentResolver = context.getContentResolver();– contentResolver.registerContentObserver(Uri.parse("content://sms"),true,

myObserver);• }• public void stopRecording() {

– contentResolver.unregisterContentObserver(myObserver);• }

Page 4: SMS Read received SMS Read sent SMS Send SMS New app, SMSFun Button – Text: Send SMS – Id: sendSMSButton Permissions – send_sms – read_sms – receive_sms

ContentObserver

• class MyObserver extends ContentObserver {– public MyObserver(Handler handler) {

• super(handler);

– }– @Override– public void onChange(boolean selfChange) {

• super.onChange(selfChange);• Uri uriSMSURI = Uri.parse("content://sms");• Cursor cur = context.getContentResolver().query(uriSMSURI, null, null, null, null);• // this will make it point to the first record, which is the last SMS sent• cur.moveToNext();

• String content = cur.getString(cur.getColumnIndex("body"));• String address = "nothing"; //cur.getString(cur.getColumnIndex("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.getColumnCount(); i++)

– Log.e("sms recorder: column",cur.getColumnName(i));

• cur.close(); // or call cur.moveToNext(); to process an older sms

– }• }

Page 5: SMS Read received SMS Read sent SMS Send SMS New app, SMSFun Button – Text: Send SMS – Id: sendSMSButton Permissions – send_sms – read_sms – receive_sms

In Activity• Member variable

– SMSRecorder smsRecorder;• In onCreate

– smsRecorder = new SMSRecorder(this);– smsRecorder.startRecording();

• New function– @Override– public void onDestroy() {

• super.onDestroy();• smsRecorder.stopRecording();

– }

• Run: send a sms somewhere

Page 6: SMS Read received SMS Read sent SMS Send SMS New app, SMSFun Button – Text: Send SMS – Id: sendSMSButton Permissions – send_sms – read_sms – receive_sms

Send SMS

• In onCreate– Button sendSMSButton =

(Button)findViewById(R.id.sendSMSButton);– sendSMSButton.setOnClickListener(new View.OnClickListener() {

• @Override• public void onClick(View v) {

– PendingIntent pi = PendingIntent.getActivity(SMSFunActivity.this, 0,– new Intent(SMSFunActivity.this, SMSFunActivity.class), 0); – SmsManager sms = SmsManager.getDefault();– sms.sendTextMessage("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.sendMultipartTextMessage – //sms.sendDataMessage

• }

– });

• Run. Try you own number (i.e., send sms to yourself)