60 likes | 234 Views
SMS. Read received SMS Read sent SMS Send SMS New app, SMSFun Button Text: Send SMS Id: sendSMSButton Permissions send_sms r ead_sms receive_sms
E N D
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.
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 (inti=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
Recording sent SMS • New class SMSRecorder • Member variables • Context context; • Handler handler; • ContentResolvercontentResolver; • public MyObservermyObserver; • 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); • }
ContentObserver • class MyObserver extends ContentObserver { • public MyObserver(Handler handler) { • super(handler); • } • @Override • public void onChange(booleanselfChange) { • 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("smsrecorder","sms sent to: "+address+" with body: "+content); • // show each column... each piece of data that could be recorded • for (inti=0; i<cur.getColumnCount(); i++) • Log.e("sms recorder: column",cur.getColumnName(i)); • cur.close(); // or call cur.moveToNext(); to process an older sms • } • }
In Activity • Member variable • SMSRecordersmsRecorder; • In onCreate • smsRecorder = new SMSRecorder(this); • smsRecorder.startRecording(); • New function • @Override • public void onDestroy() { • super.onDestroy(); • smsRecorder.stopRecording(); • } • Run: send a sms somewhere
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); • SmsManagersms = 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)