暂停在android中的后台服务

时间:2012-08-22 02:02:41

标签: android service

我正在开发一个使用服务播放媒体播放器的应用。该应用程序工作正常,但当我收到电话或消息时,音乐继续播放。我的要求是当我接到电话时,音乐应该暂停并且在通话断开时,它应该从停止的地方恢复。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

我不确定您是如何对应用进行编码的,但这对您有所帮助。 看看这个How to know the moment when the called person picks up his phone您只需要在switch语句中暂停指定事件的音乐

TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);

private PhoneStateListener mPhoneListener = new PhoneStateListener() {
    public void onCallStateChanged(int state, String incomingNumber) {
        try {
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                // do something...
                break;

            case TelephonyManager.CALL_STATE_OFFHOOK:
                // this state is the one you're looking for
                break;

            case TelephonyManager.CALL_STATE_IDLE:
                // do something...
                break;
            default:
                Log.d(TAG, "Unknown phone state=" + state);
            }
        } catch (RemoteException e) {}
    } 
};

编辑:这是另一个链接Detect if an outgoing call has been answered