Search code examples
androidandroid-activitybroadcastreceivertelephonymanager

OFF_HOOK STATE is never invoked for outgoing and incoming calls


I have written a BroadcastReceiver to detect outgoing and incoming calls. I am monitoring the network status when the user is on call (i.e when the phone state is off_hook). unfortunately off_hook state is not getting invoked for both incoming and outgoing calls.

Here is the BroadcastReceiver:

public class BroadcastReceiverImpl extends BroadcastReceiver{

    private ConnectivityManager connectivityManager;
    private TelephonyManager telephonyManager;
    private NetworkInfo networkInfo;
    private String networkStauts = "Not connected !!";

    //java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime().
    @Override
    public void onReceive(final Context context, Intent intent) {

        Toast.makeText(context, "Inside onReceive Method !!", Toast.LENGTH_SHORT).show();

        connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        networkInfo = connectivityManager.getActiveNetworkInfo();

        final Intent activityIntent = new Intent(context, ActivityImpl.class);

        telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(new PhoneStateListener(){

            @Override
            public void onCallStateChanged(int state, String incomingNumber) {


                switch (state) {

                case TelephonyManager.CALL_STATE_RINGING:
                     Toast.makeText(context, "Call State Ringing !!", Toast.LENGTH_SHORT).show();   
                    break;

                case TelephonyManager.CALL_STATE_OFFHOOK:
                    Toast.makeText(context, "Call State Offhook !!", Toast.LENGTH_SHORT).show();
                    while (networkInfo!=null && networkInfo.isConnectedOrConnecting()) {
                        networkStauts = "Network Connected";
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    break;

                case TelephonyManager.CALL_STATE_IDLE:
                    Toast.makeText(context, "Call State Idle !!", Toast.LENGTH_SHORT).show();
                    activityIntent.putExtra("networkStatus", networkStauts);
                    activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(activityIntent);
                    abortBroadcast();
                    break;
                default:
                    break;
                }

                super.onCallStateChanged(state, incomingNumber);
            }   




        }, PhoneStateListener.LISTEN_CALL_STATE);

    }


}

Every time the call is over I am displaying the network status via an activity. But unfortunately it is displaying " Not connected !! " for every call (outgoing/incoming). Please let me know what the problem is.

my manifest file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.monitornetwork_v10"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity android:name="com.example.activity.NetworkMonitorActivityImpl"></activity>

        <receiver
            android:name="com.example.broadcastreceiver.BroadcastReceiverImpl"
            android:enabled="true" >
            <intent-filter>
                <action android:name="android.intent.action.ACTION_PHONE_STATE_CHANGED" />
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
                <action android:name="android.intent.action.ANSWER" />
                <action android:name="android.intent.action.PHONE_STATE" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
    </application>

</manifest>

Solution

  • You should extends PhoneStateListener

    TelephonyManager mTelManager;
    final MyPhoneStateListener mPhoneStateListener = new MyPhoneStateListener();
    

    In your activity onCreate

    mTelManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    mTelManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
    

    create an inner class

    protected class MyPhoneStateListener extends PhoneStateListener
    {
        @Override
        public void onCallStateChanged(int state, String incomingNumber)
        {
            switch (state)
                    {
                        case TelephonyManager.CALL_STATE_RINGING:
    
                           // do whatever you want here
                            break;
    
                        case TelephonyManager.CALL_STATE_OFFHOOK:
                            // do whatever you want here
                            break;
    
                        case TelephonyManager.CALL_STATE_IDLE:
                            // do whatever you want here
                    }
    
        }
    }
    

    in your manifest file add

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