receive sms from my application in android from special number -
i want program able receive sms special number("+9856874236"). but, if sms other number, should go phone's message inbox , not application.
please me dears
import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.os.bundle; import android.telephony.smsmessage; import android.util.log; public class receiver extends broadcastreceiver { public string str = ""; @override public void onreceive(context context, intent intent) { // ---get sms message passed in--- bundle bundle = intent.getextras(); smsmessage[] msgs = null; if (bundle != null) { object[] pdus = (object[]) bundle.get("pdus"); msgs = new smsmessage[pdus.length]; (int = 0; < msgs.length; i++) { msgs[i] = smsmessage.createfrompdu((byte[]) pdus[i]); //for sms special number=============================== string msg_from = msgs[i].getoriginatingaddress(); log.v("msg_from >>",msg_from); if(msg_from.equals("+9830007546")) { //=============================== str += "sms " + msgs[i].getoriginatingaddress(); str += " :"; str += msgs[i].getmessagebody().tostring(); str += "\n"; intent act = new intent(context, receivesms.class); act.addflags(intent.flag_activity_new_task); act.putextra("message", str); context.startactivity(act); this.abortbroadcast(); } } // ---display new sms message--- // toast.maketext(context, str, toast.length_short).show(); } } }
main class :
private textview showsms; private string receivedsms; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); //this.setcontentview(r.layout.receivesms); /*removing system bar screen*/ this.getwindow().setflags(windowmanager.layoutparams.flag_fullscreen, windowmanager.layoutparams.flag_fullscreen); setcontentview(r.layout.receivesms); intent act = getintent(); receivedsms = act.getstringextra("message"); showsms = (textview) this.findviewbyid(r.id.textview2); showsms.settext(receivedsms);
give broadcastreceiver
high priority in androidmanifest
. put receiver first in line process broadcast.
<receiver android:name=".smsreceiver"> <intent-filter android:priority="1000"> <action android:name="android.provider.telephony.sms_received" /> </intent-filter> </receiver>
inside broadcastreceiver
, check if number of incoming sms equals given number. if true, cancel broadcast abortbroadcast()
.
edit
there couple of things watch out for:
- when number matching, aware of phone number formatting. if incoming number
0123456789
, usestring.equals('+xx0123456789')
, check fail, though technically correct. have @ phonenumberutils find solution this. - if recall correctly, device manufacturers have given default sms application priority 1 on processing sms broadcasts. mess application. sure test out ;)
Comments
Post a Comment