Android READ_PHONE_STATE事件

时间:2014-07-28 12:32:48

标签: java android loops broadcast receiver

我可以在READ_PHONE_STATE事件中使用循环吗?

我希望在手机开始响铃以结束响铃时循环播放。 与用户开始呼叫断开呼叫的情况一样。

这是我的广播接收器类

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        while (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            //wate for user shaking the phone
            //call ansered
        }           
        while (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
            //do something while phone answered
            //I want to work something during the calling time
        }
        if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE) {
            //do some
        }
    }
}

2 个答案:

答案 0 :(得分:0)

你已经完成了所有必要的研究,它可能不是你想做的'循环'。

您需要的是在电话响铃时启动某些内容,并在电话停止响铃时停止播放。同样适用于通话。

要在手机响铃时处理事件,请在CALL_STATE_RINGING上开始执行某项操作,然后等待CALL_STATE_OFFHOOKCALL_STATE_IDLE知道响铃何时停止。

在用户拨打电话时也是如此:当接收方收到CALL_STATE_OFFHOOK时启动某些内容,并在收到CALL_STATE_IDLE时停止播放。

我希望您的BroadcastReceiver在您的UI线程中发送消息(或调用函数)来执行您需要执行的操作,然后在调用状态发生更改时通知。

您不需要在这些时间间隔内循环。你只想在合适的时间开始/停止做某事。你可以随时提供一些备份安全性(例如:如果电话响了,3分钟没有发生任何事情,我就会停止做某事)以确保你不想继续运行。

答案 1 :(得分:0)

您可以检测何时接听/拒绝来电,以及何时开始/停止拨出电话,以实施您的"循环播放"相关回调中的逻辑。我在下面的代码中包含了评论:

public abstract class PhoneCallReceiver extends BroadcastReceiver {
        static CallStartEndDetector listener;



@Override
public void onReceive(Context context, Intent intent) {
    savedContext = context;
    if(listener == null){
        listener = new CallStartEndDetector();
    }


        TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
    telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}



public class CallStartEndDetector extends PhoneStateListener {
    int lastState = TelephonyManager.CALL_STATE_IDLE;
    boolean isIncoming;

    public PhonecallStartEndDetector() {}


    //Incoming call-   IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when hung up
    //Outgoing call-  from IDLE to OFFHOOK when dialed out, to IDLE when hunged up

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);
        if(lastState == state){
            //No change
            return;
        }
        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                isIncoming = true;
                 //incoming call started
                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                //Transition of ringing->offhook are pickups of incoming calls.  Nothing down on them
                if(lastState != TelephonyManager.CALL_STATE_RINGING){
                    isIncoming = false;
                   //outgoing call started
                }
                break;
            case TelephonyManager.CALL_STATE_IDLE:
                //End of call(Idle).  The type depends on the previous state(s)
                if(lastState == TelephonyManager.CALL_STATE_RINGING){
                    // missed call
                }
                else if(isIncoming){
                      //incoming call ended
                }
                else{
                   //outgoing call ended                                              
                }
                break;
        }
        lastState = state;
    }

}

}