How to detect incoming call with the help of Broadcast Receiver?

后端 未结 1 1497
庸人自扰
庸人自扰 2021-01-23 06:29

I\'m trying to recognize incoming calls in thru a broadcast receiver. I\'m UNABLE to do so! Infact, I\'m unable to \'trigger\' the broadcast!

Here\'s my code:

         


        
1条回答
  •  盖世英雄少女心
    2021-01-23 07:19

    intent.getAction()=="android.intent.action.PHONE_STATE"
    

    should be

    TelephonyManager.ACTION_PHONE_STATE_CHANGED.equals(intent.getAction());
    

    Since this is how you compare Strings (with equals()).

    Also, the code you use to broadcast, should never broadcast - there is no ".BroadcastMM" action. Try making an explicit one instead:

     Intent intent = new Intent(v.getContext(),BroadcastMM.class);  
     sendBroadcast(intent);
    

    It is also likely that you can't broadcast android.intent.action.PHONE_STATE, so your if won't be executed if you make an explicit Intent.

    If you really want to check that your BroadcastReceiver is working, put printouts/Toasts outside all ifs. Then once you establish that the BroadcastReceiver responds, do your check. Keep in mind though, that since you only listen for one Intent-Filter, the if checking if the Intent is a PHONE_STATE Intent is a bit redundant.

    0 讨论(0)
提交回复
热议问题