-1

I am try to register and unregister the phone state broadcast receiver in a service but its not firing at all i have declared the phone state permission in manifest as well.

Here is my broadcast receiver.

    public class IncomingCalls extends BroadcastReceiver {
    private static String mLastState;

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(ApplicationUtils.TAG, "IncomingCalls");
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        if (!state.equals(mLastState)) {
            mLastState = state;
            Log.i(ApplicationUtils.TAG, state);
            if (mLastState==TelephonyManager.EXTRA_STATE_RINGING) {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(phoneNumber, null, CallListenService.MESSAGE, null, null);

            }
        }
    }
}

and here is the service which regitser and unregister the broadcast.

    public class CallListenService extends Service {

    private static final String ACTION = "android.intent.action.PHONE_STATE";
    static IncomingCalls myReceiver;
    public  static String MESSAGE;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        final IntentFilter theFilter = new IntentFilter();
        theFilter.addAction(ACTION);
        myReceiver = new IncomingCalls();
        // Registers the receiver so that your service will listen for
        // broadcasts
        this.registerReceiver(myReceiver, theFilter);

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        MESSAGE = intent.getStringExtra(KEY_MESSAGE);
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        // Do not forget to unregister the receiver!!!
        this.unregisterReceiver(myReceiver);
    }
}

That is how i start the service

Intent intent1 = new Intent(context, CallListenService.class);
                        intent1.putExtra(KEY_MESSAGE, msg);
                        context.startService(intent1);

It was working before but it's not working at all now.

David Wasser
  • 93,459
  • 16
  • 209
  • 274

1 Answers1

0

I find the bug was related to the android 6.0 I need to ask your for READ_PHONE_STATE permission and after that braodcast receiver started firing.