android - How to send/receive broadcast between service and activity -
i need send message smsreceiver
class main_activity , can't it. tried out , searched but... .
here code
smsreceiver.java
public class smsreceiver extends broadcastreceiver { // object of smsmanager final smsmanager sms = smsmanager.getdefault(); public void onreceive(context context, intent intent) { // retrieves map of extended data intent. final bundle bundle = intent.getextras(); try { if (bundle != null) { final object[] pdusobj = (object[]) bundle.get("pdus"); (int = 0; < pdusobj.length; i++) { smsmessage currentmessage = smsmessage.createfrompdu((byte[]) pdusobj[i]); string phonenumber = currentmessage.getdisplayoriginatingaddress(); string sendernum = phonenumber; string message = currentmessage.getdisplaymessagebody(); log.i("smsreceiver", "sendernum: "+ sendernum + "; message: " + message); intent intent1=new intent(context, mainactivity.class); intent1.putextra("m",message); context.sendbroadcast(intent1); } // end loop } // bundle null } catch (exception e) { log.e("smsreceiver", "exception smsreceiver" +e); } } }
mainactivity.java
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final edittext intext=(edittext) findviewbyid(r.id.edittext2inputtext); broadcastreceiver mysms=new broadcastreceiver() { @override public void onreceive(context arg0, intent arg1) { string sms=arg1.getextras().getstring("m"); intext.settext(sms); } }; } }
instead of context.sendbroadcast(intent1);
should try context.startactivity(intent1);
try
intent = new intent(context,main.class); i.addflags(intent.flag_activity_new_task); context.getapplicationcontext().startactivity(i);
Comments
Post a Comment