android - How get contacts info into sms broadcast receiver class? -
i have app works contacts info when cell phone receive sms, need contacts info sms receiver class create problem when sms receiver fetch contacts gave null pointer exception error because don't fetch contacts info, idea contacts info compare number of sms sender id other actions in app.
here's code:
public class smsbroadcastreceiver extends broadcastreceiver { private static final string sms_received = "android.provider.telephony.sms_received"; private static final string tag = "smsbroadcastreceiver"; private static activity mactivity; dbhelper mdbhelper; private context mcontext; private arraylist<contact> mcontactsarraylist; public string str4; private contactmanager acontactmanager; private arraylist<contact> ctnlist; @override public void onreceive(context context, intent intent) { //---get sms message passed in--- log.d("smsbroadcastreceiver", "yes calls onreceive"); bundle bundle = intent.getextras(); smsmessage[] msgs = null; string str = ""; string str2 = ""; if (bundle != null) { //---retrieve sms message received--- object[] pdus = (object[]) bundle.get("pdus"); msgs = new smsmessage[pdus.length]; (int i=0; i<msgs.length; i++){ msgs[i] = smsmessage.createfrompdu((byte[])pdus[i]); str2=msgs[i].getoriginatingaddress(); str = msgs[i].getmessagebody().tostring(); str4 = str2; } if(str.charat(0)=='#'&&str.length()<=4){ string ns = context.notification_service; notificationmanager notmanager = (notificationmanager) context.getsystemservice(ns); //configuramos la notificaci�n int icono = r.drawable.resultadosmnu; charsequence textoestado = "ask-it notification"; long hora = system.currenttimemillis(); notification notif = new notification(icono, textoestado, hora); charsequence titulo = "new set of response on askit"; charsequence descripcion = "there's available new set of response on survey_name graph"; intent notintent = new intent(context, proyectoaskitactivity.class); pendingintent contintent = pendingintent.getactivity( context, 0, notintent, 0); notif.setlatesteventinfo( context, titulo, descripcion, contintent); notif.flags |= notification.flag_auto_cancel; notmanager.notify(icono, notif); contact cnt = querydetailsforcontactnumber(str2); toast toast1 = toast.maketext(context, cnt.getphonenumber(), toast.length_short); toast1.show(); } } } public contact querydetailsforcontactnumber(string acontactid) { final string[] projection = new string[] { phone._id, // contact id column phone.display_name, // name of contact phone.number // id of column in data table image }; final cursor contact = mactivity.managedquery( phone.content_uri, projection, phone.number+"="+acontactid, // filter entries on basis of contact id null, // parameter contact id column compared null); if(contact.movetofirst()) { final int contactid = contact.getint( contact.getcolumnindex(phone._id)); final string name = contact.getstring( contact.getcolumnindex(phone.display_name)); final string number = contact.getstring( contact.getcolumnindex(phone.number)); final contact acontact = new contact(contactid, name,null,false,number); return acontact; } return null; } }
finally solved problem, create object class called contacts contacts parametres , create arraylist of contacts , populate contacts info compare 1 one info need.
public class smsbroadcastreceiver extends broadcastreceiver { private static final string sms_received = "android.provider.telephony.sms_received"; private static final string tag = "smsbroadcastreceiver"; dbhelper mdbhelper; static context mcontext; private arraylist<contact> ctnlist; @override public void onreceive(context context, intent intent) { //---get sms message passed in--- log.d("smsbroadcastreceiver", "yes calls onreceive"); bundle bundle = intent.getextras(); smsmessage[] msgs = null; string str = ""; string str2 = ""; if (bundle != null) { //---retrieve sms message received--- object[] pdus = (object[]) bundle.get("pdus"); msgs = new smsmessage[pdus.length]; (int i=0; i<msgs.length; i++){ msgs[i] = smsmessage.createfrompdu((byte[])pdus[i]); str2=msgs[i].getoriginatingaddress(); str = msgs[i].getmessagebody().tostring(); } if(str.charat(0)=='#'&&str.length()<=4){ string ns = context.notification_service; ctnlist=queryallcontactswithphone(context); contact ctn = new contact(); int x = ctnlist.size(); int y = 0; while(x-1>=y){ ctn = ctnlist.get(y); if(ctn.getphonenumber().compareto(str2)==0){ toast toast1 = toast.maketext(context, ctn.getname(), toast.length_long); toast1.show(); }else{ toast toast1 = toast.maketext(context, str2, toast.length_long); toast1.show(); } y++; } } } } public arraylist<contact> queryallcontactswithphone(context ct) { final string[] projection = new string[] { phone.contact_id, // contact id column phone.display_name, // contact display name phone.number }; final string selection = contacts.has_phone_number; // contact has have phone number. final string sort = contacts.display_name+" asc"; // contact has have phone number. final cursor contacts = ct.getcontentresolver().query( phone.content_uri, // uri contact provider projection, null, // if selection = null, retrieve entries null, // not required because selection not contain parameters sort); // not order final int contactidcolumnindex = contacts.getcolumnindex(phone.contact_id); final int contactdisplaynamecolumnindex = contacts.getcolumnindex(phone.display_name); final int contactphonecolumnindex = contacts.getcolumnindex(phone.number); final arraylist<contact> contactsarraylist = new arraylist<contact>(); if(contacts.movetofirst()) { // move cursor first entry while(!contacts.isafterlast()) { // still valid entry left? final int contactid = contacts.getint(contactidcolumnindex); final string contactdisplayname= contacts.getstring(contactdisplaynamecolumnindex); final string phones = contacts.getstring(contactphonecolumnindex); final contact acontact = new contact(); acontact.setphonenumber(phones); acontact.setname(contactdisplayname); acontact.setid(contactid); contactsarraylist.add(acontact); contacts.movetonext(); // move next entry } } contacts.close(); return contactsarraylist; } }
Comments
Post a Comment