13

I'm trying to set up PhoneStateListener but I get a PhoneCallListener cannot be resolved to a type.

public class ButtonView extends FrameLayout  {

     PhoneCallListener phoneListener = new PhoneCallListener();
     TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
     telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
}

In another Example, I found its written like this and it's working

public class MainActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {

        // add PhoneStateListener
        PhoneCallListener phoneListener = new PhoneCallListener();
        TelephonyManager telephonyManager = (TelephonyManager) this
            .getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(phoneListener,PhoneStateListener.LISTEN_CALL_STATE);
}

What should I change in my code to make it working?

Shashank Agrawal
  • 25,161
  • 11
  • 89
  • 121
igor
  • 716
  • 1
  • 9
  • 27
  • http://stackoverflow.com/questions/5345470/onclicklistener-cannot-be-resolved-to-a-type-eclipse – Omarj Nov 15 '12 at 10:36

1 Answers1

26

You have to create a receiver to catch phone calls.

To do this, add this in your in manifest.xml:

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

<receiver android:name=".ServiceReceiver">
    <intent-filter>
      <action android:name="android.intent.action.PHONE_STATE" />
   </intent-filter>
</receiver>

and create these classes:

import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.webkit.WebView;

public class MyPhoneStateListener extends PhoneStateListener {

    public static Boolean phoneRinging = false;

    public void onCallStateChanged(int state, String incomingNumber) {

        switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            Log.d("DEBUG", "IDLE");
            phoneRinging = false;
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            Log.d("DEBUG", "OFFHOOK");
            phoneRinging = false;
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            Log.d("DEBUG", "RINGING");
            phoneRinging = true;

            break;
        }
    }

}
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class ServiceReceiver extends BroadcastReceiver {
    TelephonyManager telephony;

    public void onReceive(Context context, Intent intent) {
        MyPhoneStateListener phoneListener = new MyPhoneStateListener();
        telephony = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
    }

    public void onDestroy() {
        telephony.listen(null, PhoneStateListener.LISTEN_NONE);
    }

}
Floern
  • 33,559
  • 24
  • 104
  • 119
Talha
  • 12,673
  • 5
  • 49
  • 68
  • 2
    Just a correction in onDestroy(). You shouldn't pass null: telephony.listen(phoneListener, PhoneStateListener.LISTEN_NONE); phoneListener should be made a member. – Pin Jan 03 '14 at 18:11
  • Hi. I cannot get and log incomingNumber from the parameters. Why? – halilkaya May 04 '14 at 22:16
  • 7
    You don't have OnDestroy in Receiver. http://developer.android.com/reference/android/content/BroadcastReceiver.html – Hagai L Jan 06 '15 at 11:26
  • 5
    READ_PHONE_STATE permission - is a dangerous permission, and must be checked on runtime on marshmallow or above – Marat Jun 16 '16 at 05:10
  • Btw, if you use the listener, you dont need the permission -- only broadcast needs it as it seems – urSus Jun 25 '18 at 18:44