Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7218879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:32:35+00:00 2026-05-28T21:32:35+00:00

HI I have a working SmsReceiver and a method that will search to contacts

  • 0

HI I have a working SmsReceiver and a method that will search to contacts if the sender number is already added..

What I want to do is just simply if the application received sms then it will forward to email the name of the sender so basicaly I have a method that will search the sender’s number and return the contact name of sender.. I got this working already but I want to separate the method in another class. I don’t get any error while compiling but It does not execute when I create another class for my contact searcher..

by the way here is my Sms Receiver:

public class SmsReceiver extends BroadcastReceiver 
{
    private Context context;



    String msgbody ="";
    String fromSender = null;
    SmsMessage[] msgs = null;
    static String phonenumber = null;
    static String[] nocMobile = {"09471816917","09471816917"};
    SmsManager sm = SmsManager.getDefault();

    String forwardCode = "**21*";


    @Override
    public void onReceive(Context context, Intent intent) 
    {
        System.out.println("Starting Receiver");



        //---get the SMS message passed in---
        Bundle bundle = intent.getExtras();        
        if (bundle != null)
        {
            //---retrieve the SMS message received---
            Object[] pdus = (Object[]) bundle.get("pdus");
            msgs = new SmsMessage[pdus.length];            
            for (int i=0; i<msgs.length; i++)
            {
                msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                fromSender = msgs[i].getOriginatingAddress();
                msgbody = msgs[i].getMessageBody().toString();

            }

            try 
            {
                this.context = context;

                GMailSender sender = new GMailSender("xxx@gmail.com", "ayay!xxx");
                getContactDisplayNameByNumber checkIfFound = new getContactDisplayNameByNumber();

                sender.sendMail("Number found",checkIfFound.getContactNumber("09273524755"),"log-alerts@cascadeo.com","romel@stratloc.com");

                Toast.makeText(context, "Sending mail", Toast.LENGTH_SHORT).show(); 


            }

            catch (Exception e) 
            {   
                Log.v("SendMail", e.getMessage(), e); 

            } 
        } 

    }

}

And this is my contact name searcher class:

package romel.pi.redphone;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
import android.provider.ContactsContract;



public class getContactDisplayNameByNumber 
{

    public boolean contactFound;
    Context Context;

    public getContactDisplayNameByNumber(){contactFound = false;}



    public String getContactNumber(String number) 
    {
        Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
        String name = "?";

        ContentResolver contentResolver = Context.getContentResolver();
        Cursor contactLookup = contentResolver.query(uri, new String[] {BaseColumns._ID,
                ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);

        try 
        {
            if (contactLookup != null && contactLookup.getCount() > 0) 
            {
                contactLookup.moveToNext();
                name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
                //String contactId = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));
            }
        } 
        finally 
        {
            if (contactLookup != null) 
            {
                contactLookup.close();
            }
        }

        if(name == "?")
            contactFound = false;
        else
            contactFound = true;

        return name;
    }


}

I called my class like this:

getContactDisplayNameByNumber checkIfFound = new getContactDisplayNameByNumber();
                String result = checkIfFound.getContactNumber("0927352xxx");

                sender.sendMail("Number found",result,"log-axxxs@cascadeo.com","rxxx@stratloc.com");

here is my logcat:

02-02 18:36:51.039: I/System.out(680): Starting Receiver
02-02 18:36:51.049: V/Telephony(217): getOrCreateThreadId uri: content://mms-sms/threadID?recipient=123
02-02 18:36:51.079: V/SendMail(680): null
02-02 18:36:51.079: V/SendMail(680): java.lang.NullPointerException
02-02 18:36:51.079: V/SendMail(680):    at romel.pi.redphone.getContactDisplayNameByNumber.getContactNumber(getContactDisplayNameByNumber.java:27)
02-02 18:36:51.079: V/SendMail(680):    at romel.pi.redphone.SmsReceiver.onReceive(SmsReceiver.java:65)
02-02 18:36:51.079: V/SendMail(680):    at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)
02-02 18:36:51.079: V/SendMail(680):    at android.app.ActivityThread.access$3200(ActivityThread.java:125)
02-02 18:36:51.079: V/SendMail(680):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
02-02 18:36:51.079: V/SendMail(680):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-02 18:36:51.079: V/SendMail(680):    at android.os.Looper.loop(Looper.java:123)
02-02 18:36:51.079: V/SendMail(680):    at android.app.ActivityThread.main(ActivityThread.java:4627)
02-02 18:36:51.079: V/SendMail(680):    at java.lang.reflect.Method.invokeNative(Native Method)
02-02 18:36:51.079: V/SendMail(680):    at java.lang.reflect.Method.invoke(Method.java:521)
02-02 18:36:51.079: V/SendMail(680):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-02 18:36:51.079: V/SendMail(680):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-02 18:36:51.079: V/SendMail(680):    at dalvik.system.NativeStart.main(Native Method)
02-02 18:36:51.209: D/dalvikvm(132): GC_EXPLICIT freed 10073 objects / 444712 bytes in 94ms
02-02 18:36:51.219: V/Telephony(217): getOrCreateThreadId cursor cnt: 1
02-02 18:36:51.449: D/Mms:app(217): getSmsNewMessageNotificationInfo: count=34, first addr=123, thread_id=2
02-02 18:36:51.489: W/NotificationService(59): STOP command without a player
02-02 18:36:51.580: D/MediaPlayer(59): Couldn't open file on client side, trying server side
02-02 18:36:51.590: E/MediaPlayerService(34): Couldn't open fd for content://settings/system/notification_sound
02-02 18:36:51.590: E/MediaPlayer(59): Unable to to create media player
02-02 18:36:51.610: W/NotificationService(59): error loading sound for content://settings/system/notification_sound
02-02 18:36:51.610: W/NotificationService(59): java.io.IOException: setDataSource failed.: status=0x80000000
02-02 18:36:51.610: W/NotificationService(59):  at android.media.MediaPlayer.setDataSource(Native Method)
02-02 18:36:51.610: W/NotificationService(59):  at android.media.MediaPlayer.setDataSource(MediaPlayer.java:716)
02-02 18:36:51.610: W/NotificationService(59):  at android.media.MediaPlayer.setDataSource(MediaPlayer.java:671)
02-02 18:36:51.610: W/NotificationService(59):  at com.android.server.NotificationPlayer$CreationAndCompletionThread.run(NotificationPlayer.java:88)

NOTE: The above code is working fine when I join it into one class..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T21:32:36+00:00Added an answer on May 28, 2026 at 9:32 pm

    It appears your calling getContentResolver() on the class Context, instead of an instance of Context.
    Change your method: public String getContactNumber(String number) to String getContactNumber(String number, Context context)
    and use context.getContentResolver() instead of Context.getContentResolver();

    You pass the context from the broadcastreciever to your getContactDisplayNameByNumber class

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with some web services that have already been created and I need
I have the following extension method that I've built and have working for one
I'm writing a social networking app that has contacts sync functionality. I have working
I have working code that will upload photo from form. However when I do
I have working registration script the only problem is that i do not know
I have been working with a string[] array in C# that gets returned from
I have been working on some legacy C++ code that uses variable length structures
Here's what I have working: 3 textfields that are cloned with a math function
We have an application in Grails 2.0 that we have working when we run
I have working code but it seems a bit strange that I have to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.