Pratishtha Sharma
Pratishtha Sharma

Reputation: 111

BroadcastReceiver gets called even if the app is killed

In my app there is a BroadCastReceiver which handles the incoming calls. When there are incoming calls I call some native code methods. I dont want this broadcast receiver to be called when the app is killed after swiping. At the moment even if I kill my application the receiver is called. The code for the receiver is-

public class CallReceiver extends BroadcastReceiver{

    private final String TAG = this.getClass().getSimpleName();
    private Context context;

    public CallReceiver(){
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e(TAG, "In onReceive of CallReceiver");
        this.context = context;

        try {
            // Telephony manager object to register for the listener
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

            // Initialising the MyPhoneStateListener class
            MyPhoneStateListener PhoneListener = new MyPhoneStateListener();

            // Register listener for LISTEN_CALL_STATE
            telephonyManager.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

        } catch (Exception e) {
            Log.e("Phone Receive Error", " " + e);
        }
    }

    private class MyPhoneStateListener extends PhoneStateListener {

            @Override
            public void onCallStateChanged(int state, String incomingNumber) {
                switch (state){
                    case TelephonyManager.CALL_STATE_OFFHOOK:
                        // This state denotes that the mobile is busy in some call

                            GLLib.pause(true);

                        Log.e(TAG, "Phone busy");
                        break;

                    case TelephonyManager.CALL_STATE_RINGING:
                        // This state denotes that the phone is ringing

                            GLLib.pause(true);

                        Log.e(TAG, "Phone ringing");
                        break;

                    case TelephonyManager.CALL_STATE_IDLE:
                        // This state denoted that the phone is idle

                            GLLib.pause(false);

                        Log.e(TAG, "Phone idle");
                        break;
                }
                super.onCallStateChanged(state, incomingNumber);
            }
        }
}

In manifest I have-

      <intent-filter>
          <action android:name="android.intent.action.PHONE_STATE"/>
      </intent-filter>
  </receiver>

The permission is-

Upvotes: 3

Views: 2899

Answers (2)

Dilip
Dilip

Reputation: 2311

The simplest way to solve the issue is

 Register BroadCastReceiver in onResume, unregister in onPause

Hope this should work for you.

Upvotes: 1

Zohair
Zohair

Reputation: 336

Broadcast receivers have nothing to do with the life cycle of your application, Android reads your Application's Manifest and sends the Broadcasts to respective app expecting the particular action. If you wish to stop receiving broadcasts when App is not active then you need to unregister your broadcast in onDestroy of your Activity/Service

unregisterReceiver(BroadcastReceiver receiver)

Upvotes: 3

Related Questions