sms - Android: pass variables from Activity to BroadcastReceiver -
i have problem passing throught variable activity broadcastreceiver... here code:
here broadcast receiver code... try catch sms 1 phone number have got activity...
public class smsmonitor extends broadcastreceiver { private static final string action = "android.provider.telephony.sms_received"; public static string phone_number = ""; public static string msg_body = ""; public static final string sms_extra_name = "pdus"; @override public void onreceive(context context, intent intent) { string phone = intent.getextras().getstring("trusted_num"); if (intent != null && intent.getaction() != null && action.comparetoignorecase(intent.getaction()) == 0) { object[] pduarray = (object[]) intent.getextras().get("pdus"); smsmessage[] messages = new smsmessage[pduarray.length]; (int = 0; < pduarray.length; i++) { messages[i] = smsmessage.createfrompdu((byte[]) pduarray[i]); } phone_number = messages[0].getdisplayoriginatingaddress(); msg_body = messages[0].getmessagebody(); system.out.println("phone number: "+phone_number); system.out.println("phone entered: "+phone); } } }
here activity code:
public class settings extends activity implements onclicklistener{ private button btn_save; private edittext txt_phone; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_settings); //set save button btn_save = (button)findviewbyid(r.id.btn_save); txt_phone = (edittext)findviewbyid(r.id.et_phone); btn_save.setonclicklistener(this); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.activity_settings, menu); return true; } @override public void onclick(view v) { if (v == btn_save) { try { string phone_num = txt_phone.gettext().tostring(); intent = new intent(settings.this, smsmonitor.class); i.putextra("trusted_num", phone_num); sendbroadcast(i); } catch(exception e) { system.out.println("error: "+e.getlocalizedmessage()); } } } }
in code have text field entering phone number, need pass broadcastreceiver intent.putextra()
method, in logcat see, variable didnot pass:
07-25 18:43:57.382: i/system.out(14245): phone number: +37129690449 07-25 18:43:57.382: i/system.out(14245): phone entered: null
so doing wrong here?
upd maybe code not correct, works me...
public void onreceive(context context, intent intent) { phone = intent.getextras().getstring("trusted_num");//get trusted phone number settings screen //receive sms if (intent != null && intent.getaction() != null && action.comparetoignorecase(intent.getaction()) == 0) { object[] pduarray = (object[]) intent.getextras().get("pdus"); smsmessage[] messages = new smsmessage[pduarray.length]; (int = 0; < pduarray.length; i++) { messages[i] = smsmessage.createfrompdu((byte[]) pduarray[i]); } phone_number = messages[0].getdisplayoriginatingaddress(); msg_body = messages[0].getmessagebody(); system.out.println("phone number: "+phone_number); } //check if number not null if (phone != null && phone != "") { system.out.println("phone entered: "+phone); } } }
you can't pass intent broadcast receiver. "there no way broadcastreceiver see or capture intents used startactivity()" https://developer.android.com/reference/android/content/broadcastreceiver.html
i had similar issue while , solved using combination of intentservices , activities. have restructure program fit these guidlines
Comments
Post a Comment