Search code examples
androidbroadcastreceiverandroid-broadcast

Can't get incoming call event


I want to implement getting incoming call events in background and foreground. So I've created BroadcastReceiver for that purpose, but onReceive() method doesn't fired after I got/getting/finished incoming call. I've tryed tons of tutorial, but nothing helped me:(

Please help.

MANIFEST

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="yakiv.bondar.dev.incomeoutcomecalltest">

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver
        android:name=".IncomingCall"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>
</application>

Broadcast receiver

public class IncomingCall extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Log.d("tag", "AAAAAAAAAAAAAAAAAAAAAA");
    MyPhoneStateListener phoneListener=new MyPhoneStateListener();
    TelephonyManager telephony = (TelephonyManager)
            context.getSystemService(Context.TELEPHONY_SERVICE);
    telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}

public class MyPhoneStateListener extends PhoneStateListener {
    public void onCallStateChanged(int state,String incomingNumber){
        switch(state){
            case TelephonyManager.CALL_STATE_IDLE:
                Log.d("tag", "IDLE");
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                Log.d("tag", "OFFHOOK");
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                Log.d("tag", "RINGING");
                break;
        }
    }
}
}

Main Activity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Solution

  • You have missed calling registerReciever(intent,intentFilter) in your Activity. Register your activity in onResume() and call unregisterReciever(intent) in your onPause() method.

    NOTE:

    Also check if you have given appropriate permissions in your manifest and also note that even if you have declared your required permissions you have to handle certain permissions in runtime in Android 6.0 and above. Check here for handling permissions at runtime for Marshmallow and above.