Listen to incoming calls through BroadcastReceiver, without PhoneStateIntentReceiver or PhoneStateListener

Is there a way to listen for incoming calls by extending BroadcastReceiver to listen to the OS broadcast without using PhoneStateIntentReceiver or PhoneStateListener. Also, please tell me what the actions and permissions will be in the manifest.

I tried to work as follows, but it does not work for incoming calls, but works for outgoing

The only .java application file is as follows (the application contains only one .java file and one manifest file)

package com.crsardar.media.audio; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class IncommingCallReceiverCRS extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { Log.e("Chitta : ", "Its working"); } } 

manifest

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.crsardar.media.audio" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <receiver android:name="IncommingCallReceiverCRS" android:enabled="true"> <intent-filter> <!--action android:name="android.intent.action.NEW_OUTGOING_CALL"/--> <action android:name="android.intent.action.ANSWER" > <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver> </application> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/> <uses-permission android:name="android.permission.READ_PHONE_STATE"/> <uses-permission android:name="android.permission.RECORD_AUDIO"/> </manifest> 
+8
android android-broadcast
source share
2 answers

The action you specified in the manifest is incorrect. This is an action that can be used to answer a call and not track incoming calls.

You can use two broadcast receivers that listen on ACTION_PHONE_STATE_CHANGED and NEW_OUTGOING_CALL broadcasts.

ACTION_PHONE_STATE_CHANGED will be accepted when a new incoming call arrives, answers a call, or hangs (see the documentation for EXTRA received with this intent).

NEW_OUTGOING_CALL will be received if a new outgoing call is installed on your device.

Regarding access rights, I think you got it right in your manifest (I suppose the RECORD_AUDIO permission is used for something else in your application)

+8
source share

Here is my demo for android unit test. You can turn to him.

 public interface ICallVerify { void onOutgoing(Context context, Intent intent); void onCallStateChange(Context context, Intent intent); } protected void setUpCallVerify(final ICallVerify callVerify) { //listen ingoing and outgoing final CountDownLatch latch = new CountDownLatch(1); BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) { //state change Log.i(TAG, "outgoing call..."); callVerify.onOutgoing(context, intent); } else if (intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)){ // state changed String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); if (state.equals("RINGING")) { state += " number:" + intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); } Log.i(TAG, "call state changed.... " + state); callVerify.onCallStateChange(context, intent); } } }; IntentFilter filter = new IntentFilter(TelephonyManager.ACTION_PHONE_STATE_CHANGED); filter.addAction(Intent.ACTION_NEW_OUTGOING_CALL); ContextUtils.getTargetContext().registerReceiver(receiver, filter); try { latch.await(5, TimeUnit.MINUTES); } catch (InterruptedException e) { e.printStackTrace(); } } 

Do not forget to add permissions

 <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission> 
0
source share

All Articles