【问题标题】:How to manage the states of an outgoing call in Android?如何在 Android 中管理拨出电话的状态?
【发布时间】:2014-06-11 22:01:08
【问题描述】:

我正在为大学考试开发一个安卓应用程序。
我使用的是三星 Galaxy S3,带有原始 ROM,Android Jelly Bean 4.3。

我在检测拨出电话的“状态”时遇到问题。
我需要知道电话何时从我的应用程序“开始”和“停止”(拒绝与否),因为我必须暂停并重新启动音乐服务的播放歌曲。

我已经对来电进行了处理,并且以这种方式完美运行:

public void onReceive(Context context, Intent intent) {
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
        if (Start.getMusicService().isPlaying()) {
            pauseService();

            isMusicPlaying = true;
        }
    }
    else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
        if (isMusicPlaying){
            startService();

            isMusicPlaying = false;
        }
    }
}

对于拨出电话,我在清单中注册接收者

<receiver android:name=".Music.OutgoingCallReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        </intent-filter>
    </receiver>

并添加权限

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

我为接收器尝试了这个解决方案

public void onReceive(Context context, Intent intent) {
    TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(new PhoneStateListener() {
        public void onCallStateChanged(int state, String number) {

            if (state == TelephonyManager.CALL_STATE_IDLE) {
                if (Start.getMusicService().isPlaying()) {
                    pauseService();

                    isMusicPlaying = true;
                }
            }
            else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                if (isMusicPlaying){
                    startService();

                    isMusicPlaying = false;
                }
            }
        }
    }, PhoneStateListener.LISTEN_CALL_STATE);
}

我已经阅读了其他问题,但我没有找到答案。
我希望你能帮助我。

安德烈亚

【问题讨论】:

  • onReceive 根本没有被调用?你是如何设置广播接收器的?
  • @Rarw 我用 onReceive 方法编辑我的问题。每次都正确调用它,但在方法中我无法做我需要的事情
  • 如果你只是想在通话时调节应用的音量,你也可以使用音频焦点:developer.android.com/training/managing-audio/audio-focus.html
  • 对于来电,如果应用程序关闭并且您在主屏幕上,这是否有效?我问是因为如果此接收器处于活动状态,一旦关闭应用程序以拨打电话,它就不会处于活动状态。
  • @ValentinRocher 我想暂停歌曲并在通话结束时从同一点重新开始。

标签: android phone-call


【解决方案1】:

制作程序读取收音机的logcat, 命令:

logcat -d -b radio RILJ:D RILC:D *:S

我在 android 2.3 上,当拨出电话时得到输出:

06-11 20:38:43.051 D/RILJ    (  429): [4678]< GET_CURRENT_CALLS  [id=1,ALERTING,toa=145,norm,mo,0,voc,noevp,,cli=1,,1] 

回答后得到输出:

06-11 20:38:50.300 D/RILJ    (  429): [4683]< GET_CURRENT_CALLS  [id=1,ACTIVE,toa=145,norm,mo,0,voc,noevp,,cli=1,,1] 

所以,要检测拨出电话是否被接听, 我制定程序来捕获这些日志并检查它是否

contain time > time-when-make-outgoing-call, and
contains string "GET_CURRENT_CALLS" and "ACTIVE".

而且...它在我的 sonyericsson-android 2.3 上有效。

我不知道它是否适用于其他设备和安卓版本,你告诉我。

【讨论】:

  • 如何为上述概念编写java代码,可用于android应用程序本身?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多