java - Passing data from BroadCastRecevier to activity -
i have sms reader application , showing senderno , message body custom listview. incoming messages have registered broadcast receiver , populating listview.
whenever new message coming in broadcast receiver able want data passed onto activity.
the code snippets :
mainactvity.java
public class mainsmsactivity extends activity{ private listview smslist; smsadapter smsadapter; private smsdao smsdao; private list<smsdao> smsdatalist; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_sms_demo); smsdatalist = new arraylist<smsdao>(); intent intent = new intent(); intent.setaction("com.mobile.sms.incomingsms"); sendbroadcast(intent); populatesms(); } public void populatesms(){ uri inboxuri = uri.parse("content://sms/inbox"); string[] reqcols = new string[] { "_id", "address", "body", "date" }; contentresolver cr = getcontentresolver(); cursor cursor = cr.query(inboxuri, reqcols, null, null, null); smsdatalist.clear(); (cursor.movetofirst(); !cursor.isafterlast(); cursor.movetonext()) { smsdao = new smsdao(); smsdao.setmessagebody(cursor.getstring(1)); smsdao.setsenderno(cursor.getstring(2)); smsdao.setmessagetime(cursor.getlong(3)); smsdatalist.add(smsdao); } smsadapter = new smsadapter(this,smsdatalist); smslist.setadapter(smsadapter); smsadapter.notifydatasetchanged(); cursor.close(); } }
incomingsms.java
public class incomingsms extends broadcastreceiver { final smsmanager sms = smsmanager.getdefault(); public void onreceive(context context, intent 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(); int duration = toast.length_long; // here want send message body main activity class toast toast = toast.maketext(context, "sendernum: " + sendernum + ", message: " + message, duration); toast.show(); } // end loop } // bundle null } catch (exception e) { log.e("smsreceiver", "exception smsreceiver" + e); } }
i able receive messages intially list view want listview should automatically updated new message arrives.
in broadcasereceiver this: (use intent)
public class smsreceiver extends broadcastreceiver { public static final string notification = "receiver_sms"; @override public void onreceive(context context, intent intent) { log.i("onreceive methode", "new sms comming"); bundle mybundle = intent.getextras(); smsmessage[] messages = null; string strmessage = "", address = ""; abortbroadcast(); if (mybundle != null) { // message in pdus format(protocol description unit) object[] pdus = (object[]) mybundle.get("pdus"); // create array of messages messages = new smsmessage[pdus.length]; log.i("onreceive methode", "new sms comming"); (int = 0; < messages.length; i++) { // create smsmessage raw pdu. messages[i] = smsmessage.createfrompdu((byte[]) pdus[i]); // originating address (sender) of sms message in // string form or null if unavailable address = messages[i].getoriginatingaddress(); // message body string, if exists , text // based. strmessage += messages[i].getmessagebody(); strmessage += "\n"; } // show message in toast } // need intent broadcast = new intent(notification); broadcast.putextra("data", strmessage); localbroadcastmanager.getinstance(context).sendbroadcast(broadcast); }
and register ur receiver in ur activity
public broadcastreceiver receiver = new broadcastreceiver() { @override public void onreceive(context context, intent intent) { log.e(tag, "sms received."); // intent = getintent(); bundle b = intent.getbundleextra("sms"); // string bun = b.getstring("mydata"); log.i(tag, "bundle: " + b); string str = intent.getstringextra("data"); parsesmsdata(str); } };
and in onresume():
@override protected void onresume() { super.onresume(); localbroadcastmanager.getinstance(this).registerreceiver(receiver, new intentfilter(smsreceiver.notification)); }
and in ondestroy() must unregister receiver this:
@override protected void ondestroy() { // unregister since activity closed. localbroadcastmanager.getinstance(this).unregisterreceiver(receiver); super.ondestroy(); }
and don't forget add in ur manifest file in application tag:
<receiver android:name=".smsbroadcastreceiver"> <intent-filter> <action android:name="android.provider.telephony.sms_received"></action> </intent-filter> </receiver>
Comments
Post a Comment