android - How to get contact name when receiving SMS -


i have following code receive sms , trying contact name.

package com.example.smstest;  import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.database.cursor; import android.net.uri; import android.os.bundle; import android.provider.contactscontract; import android.provider.contactscontract.phonelookup; import android.telephony.smsmessage; import android.widget.toast;  public class smsreceiver extends broadcastreceiver {     private sqliteadapter mysqliteadapter;      @override     public void onreceive(context context, intent intent) {         mysqliteadapter = new sqliteadapter(context);         mysqliteadapter.opentoread();          message message = null;          bundle extras = intent.getextras();         if (extras == null)             return;          object[] pdus = (object[]) extras.get("pdus");         (int = 0; < pdus.length; i++) {             smsmessage smessage = smsmessage.createfrompdu((byte[]) pdus[i]);             string sender = smessage.getoriginatingaddress();             string body = smessage.getmessagebody().tostring();              message = mysqliteadapter.createmessage(sender, body);              // custom intent used broadcast             intent in = new intent("smsmessage.intent.main").putextra(                     "get_msg", sender + ":" + body);              // display toast whenever there sms.             toast.maketext(context, body, toast.length_long).show();              uri personuri = uri.withappendedpath( contactscontract.phonelookup.content_filter_uri, smessage.getoriginatingaddress());                cursor cur = context.getcontentresolver().query(personuri, new string[] { phonelookup.display_name }, null, null, null );                if( cur.movetofirst() ) {                            int nameindex = cur.getcolumnindex(phonelookup.display_name);                             string personname = cur.getstring(nameindex);                            toast.maketext(context, personname, toast.length_long).show();             }             cur.close();              // can place check conditions here(on sms or             // sender)             // , send broadcast             context.sendbroadcast(in);              // used abort broadcast , can used silently             // process incoming message , prevent further being             // broadcasted. avoid this, not way program             // app.             this.abortbroadcast();         }     } } 

this code have added cause app crash:

uri personuri = uri.withappendedpath( contactscontract.phonelookup.content_filter_uri, smessage.getoriginatingaddress());    cursor cur = context.getcontentresolver().query(personuri, new string[] { phonelookup.display_name }, null, null, null );    if( cur.movetofirst() ) {                int nameindex = cur.getcolumnindex(phonelookup.display_name);                 string personname = cur.getstring(nameindex);                toast.maketext(context, personname, toast.length_long).show(); } cur.close(); 

i modified code answer on link.

now app crashes when receiving sms.

try code

in broadcast receiver

 public class smsreceiver  extends broadcastreceiver{   string str = "";      string no = ""; @override public void onreceive(context context, intent intent) {     // todo auto-generated method stub       bundle bundle = intent.getextras();           smsmessage[] msgs = null;         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]);                                  str += msgs[i].getmessagebody().tostring();                 str += "\n";                     no = msgs[i].getoriginatingaddress();                  //resolving contact name contacts.                 uri lookupuri = uri.withappendedpath(phonelookup.content_filter_uri, uri.encode(no));                 cursor c = context.getcontentresolver().query(lookupuri, new string[]{contactscontract.data.display_name},null,null,null);                 try {                     c.movetofirst();                  string  displayname = c.getstring(0);                 string contactname = displayname;                    toast.maketext(context, contactname, toast.length_long).show();                  } catch (exception e) {                     // todo: handle exception                 }finally{                     c.close();                 }              }         }     } } 

you have register broadcast receiver in manifest intentfilter

    <receiver android:name="smsreceiver">           <intent-filter>               <action android:name= "android.provider.telephony.sms_received" />          </intent-filter>       </receiver> 

also have add user permission like

<uses-permission android:name="android.permission.send_sms"/> <uses-permission android:name="android.permission.receive_sms"/> <uses-permission android:name="android.permission.read_contacts"/> 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -