sap
sap

Reputation: 319

Phone state listener using broadcast receiver in android not working

I am trying to detect Incoming call using Broadcast Listener.Since I received one fault in my app when someone calls, my app still plays the song. So I want to pause song during Incoming Call. But I am not receiving any response.

Here is the complete code So that you all can understand where am I messing.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<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">
        <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>
</application>

IncomingCall.java

package com.example.suraj.freewee;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class IncomingCall extends BroadcastReceiver {

private String LOG_TAG = getClass().getSimpleName();
TelephonyManager telephonyManager;
Context context;

@Override
public void onReceive(Context context, Intent intent) {
    try {
        this.context = context;
        telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        MyPhoneStateListener phoneListener = new MyPhoneStateListener();
        telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);

        Log.e(LOG_TAG, "inside on receive");
    } catch (Exception e) {
        e.printStackTrace();
        Log.e(LOG_TAG, e.toString());
    }


}

private class MyPhoneStateListener extends PhoneStateListener {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        Log.e(LOG_TAG, "Number : " + incomingNumber);
        try {
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING: {
                    //PAUSE
                    SongsAdapter.player.pause();
                    Log.e(getClass().getSimpleName(), "call ringing");
                    break;
                }
                case TelephonyManager.CALL_STATE_OFFHOOK: {
                    SongsAdapter.player.pause();
                    Log.e(getClass().getSimpleName(), "call offhook");
                    break;
                }
                case TelephonyManager.CALL_STATE_IDLE: {
                    //PLAY
                    SongsAdapter.player.start();
                    Log.e(getClass().getSimpleName(), "call idle");
                    break;
                }
                default: {
                }
            }
        } catch (Exception ex) {
            Log.e(getClass().getSimpleName(), "Error: " + ex.toString());
        }
    }
}

}

In logcat no log errors are shown So why is this giving me such behaviour. thanks in advance

Upvotes: 2

Views: 2620

Answers (2)

srinivas
srinivas

Reputation: 72

try editing onReceive() as below

public void onReceive(Context context, Intent intent)    
{
    TelephonyManager phoneManager =
        (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

    if(phoneManager.getCallState() == TelephonyManager.CALL_STATE_RINGING) 
    {
        string fromNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Log.i(TAG, "u have a call from a number " + fromNumber);
    }
}

Now call to ur number and see the number in logs

Upvotes: 0

InTwoMinds
InTwoMinds

Reputation: 675

You're registering listener inside onReceive. That's pointless. As Android documentation states:

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active.

It should be enough to get EXTRA_STATE from Intent in onReceive and do your switch there.

If you're still not getting anything logged trim it to minimal case - leave only Log statement inside onReceive. Does it work? AFAIK there are two cases when app won't receive broadcast - If it was never started or if it was force stopped.

Upvotes: 2

Related Questions