0

I am trying to create an application that will perform different functions when a call is received. To make a small working example, I have made my class extend BroadcastReceiver and I have tried to get a toast notification to show up.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class IncomingCallInterceptor extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "Do something.", Toast.LENGTH_LONG).show();
    }
}

I have added this permission in my AndroidManifest.xml file:

<application android:icon="@drawable/icon" android:label="Incoming Call Interceptor">
    <receiver android:name="IncomingCallInterceptor">
        <intent-filter>
             <action android:name="android.intent.action.PHONE_STATE"/>
        </intent-filter>
    </receiver>
</application>

My test device is running Android 4.4.2. No toast notification ever shows up when someone is calling in.

raphnguyen
  • 3,565
  • 18
  • 56
  • 74

2 Answers2

0

Try This Code To Monitor the state of the Phone

import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
public class PhoneReceiver extends PhoneStateListener {
Context context;
public PhoneReceiver(Context context) {
this.context = context;
}
@Override
public void onCallStateChanged(int state, 
String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
Toast.makeText(context, "onCallStateChanged state=" + 
state + "incomingNumber=" + incomingNumber, 
Toast.LENGTH_LONG).show(); 
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(context, "idle", 
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(context, "ringing", 
Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(context, "offhook", 
Toast.LENGTH_LONG).show();
break;
}
}
}
CodeWalker
  • 2,281
  • 4
  • 23
  • 50
0

Found the answer from a similar thread: Incoming call broadcast receiver not working (Android 4.1)

You must manually start an activity from your application before the broadcast receivers starts to work as of Android 3.0.

Community
  • 1
  • 1
raphnguyen
  • 3,565
  • 18
  • 56
  • 74