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

  • Home
  • SEARCH
  • 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 9226361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:48:42+00:00 2026-06-18T04:48:42+00:00

I have a MyService class which implements custom CallReceiver.Listener . The problem is listener

  • 0

I have a MyService class which implements custom CallReceiver.Listener . The problem is listener registration called in MyService is not working. mListener in CallReceiver is always null. It proves that listener was not registerd, but I have no idea why is that.

MyService class (an extract):

public class MyService extends Service implements CallReceiver.Listener {

CallReceiver mCallReceiver;

   public int onStartCommand (Intent intent, int flags, int startId) {

    serviceStart();
    return startId;
   }

    // starting service
    public void serviceStart()  {

    mCallReceiver = new CallReceiver();

    // registering listener for CallReceiver
    mCallReceiver.registerListener(this);
   }


    public void onPhoneStateChange(boolean state) {
     if (state) {
        Log.v("onPhoneStateChange", "service destroyed");
        serviceDestroy();
    } else {
        // serviceStart();
    }
  }

}

CallReceiver class:

public class CallReceiver extends BroadcastReceiver   {

private boolean isRinging = false;
private Listener mListener = null;

// interface declaration
 public interface Listener {
        public void onPhoneStateChange(boolean state);
    }

// listener registration
 public void registerListener (Listener listener) {
        mListener = listener;
    }


@Override
public void onReceive(Context context, Intent intent) {

        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        if(TelephonyManager.EXTRA_STATE_RINGING.equals(state))  {
            // do something while ringing
                Log.v("phone", "ringing");
                isRinging = true;
        }


        else if(TelephonyManager.EXTRA_STATE_IDLE.equals(state))    {
            //do something when the call ends
            Log.v("phone", "idle");
            isRinging = false;
        }


        if(mListener != null)   {
            // onPhoneStateChange is not called because mListener is null
            mListener.onPhoneStateChange(isRinging);
        }

  }

}
  • 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-06-18T04:48:43+00:00Added an answer on June 18, 2026 at 4:48 am

    You should register your receiver dynamically.

    // registering listener for CallReceiver
       mCallReceiver.registerListener(this);
       IntentFilter filter = new IntentFilter("your.Intent");
       registerReceiver (mCallReceiver, filter);
    

    Also, in your services onDestroy(), don’t forget to unregister it.

    @Override
    public void onDestroy ()
    {
        try {
        unregisterReceiver (broadcastReceiver);
        }
        catch (IllegalArgumentException e){}
        catch (NullPointerException ne){}
        super.onDestroy();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web service that accepts and returns a custom class. The problem
I have a service class MyService which is defined and being used in controller
I have a WCF Service (called myservice.svc) that takes a message from a user
have written this little class, which generates a UUID every time an object of
I have a class A which has the following: public class A { [Import(typeof(IMyService)]
I have a centralized socket class which is responsible for sending and retrieving data.
I have a service called myService.svc. This service is exposed using a webHttpBinding binding.
I have a web service that looks something like this: [ServiceContract] public class MyService
I have service which is not invoked by user request, but by task. Now
In my Android application I have an Observable Data class, which contains a method

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.