Add PhoneStateListener

前端 未结 1 2051
死守一世寂寞
死守一世寂寞 2020-11-27 18:42

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

public class ButtonView extend         


        
相关标签:
1条回答
  • 2020-11-27 19:27

    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);
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题