1 / 6

SMS

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

kaz
Télécharger la présentation

SMS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 • // 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.

  2. 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

  3. 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); • }

  4. 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 • } • }

  5. 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

  6. 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)

More Related