如何知道我是否正在使用Android进行通话?

时间:2011-05-10 10:57:25

标签: java android telephonymanager

我想知道我是否正在接听电话。

如果我正在通话,则启动该服务(服务部分已清除)。我该怎么做?

在接听电话时我需要拨打电话......我不知道该怎么做?有什么帮助吗?

4 个答案:

答案 0 :(得分:53)

你需要广播接收器......

在清单声明广播接收器......

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

同时声明uses-permission ...

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

广播接收机类......

package x.y;

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

public class PhoneStateBroadcastReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {

        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);

    }

}

一个类来定制手机状态监听器...

package x.y;
import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class CustomPhoneStateListener extends PhoneStateListener {

    //private static final String TAG = "PhoneStateChanged";
    Context context; //Context to make Toast if required 
    public CustomPhoneStateListener(Context context) {
        super();
        this.context = context;
    }

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);

        switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            //when Idle i.e no call
            Toast.makeText(context, "Phone state Idle", Toast.LENGTH_LONG).show();
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            //when Off hook i.e in call
            //Make intent and start your service here
            Toast.makeText(context, "Phone state Off hook", Toast.LENGTH_LONG).show();
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            //when Ringing
            Toast.makeText(context, "Phone state Ringing", Toast.LENGTH_LONG).show();
            break;
        default:
            break;
        }
    }
}

答案 1 :(得分:29)

TelephonyManager.getCallState()返回

之一
  • CALL_STATE_IDLE
  • CALL_STATE_OFFHOOK
  • CALL_STATE_RINGING

如果这符合您的要求,它的代码要比Pied Piper更全面的解决方案少得多。

答案 2 :(得分:1)

答案 3 :(得分:0)

毫无疑问,这是一个古老的问题。但它作为Google的第一个结果弹出。因此,我决定添加另一种有用的方法。

更改电话状态后,系统将发送TelephonyManager.ACTION_PHONE_STATE_CHANGED广播。此广播具有额外状态,可以使用TelephonyManager.EXTRA_STATE提取。

额外状态可以有三种选择:

  • EXTRA_STATE_IDLE
  • EXTRA_STATE_OFFHOOK
  • EXTRA_STATE_RINGING

您可以设计一个广播接收机来处理所有这些问题:

public class PhoneStateReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent){

        if (intent.getAction().equals(TelephonyManager.ACTION_CALL_STATE_CHANGED){

            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

            if (state.equals(TelephonyManager.EXTRA_STATE_IDLE){

                // No call currently active.

            } else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING){

                // A call is ringing

            } else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK){

                // Call has been answered.

            }
        }
    }
}

您可以在此处了解更多信息:

https://developer.android.com/reference/android/telephony/TelephonyManager#ACTION_PHONE_STATE_CHANGED

注意:广播接收器将在后台触发,从Android O +开始,您无法从后台进程启动后台服务。考虑使用context.startForegroundService(intent)启动前台服务。在服务的onStartCommand(...)中,调用startForeground(...)。否则,系统会引发致命异常。但是,在Android O以下,您可以安全地使用context.startService(intent)而不是startForegroundService(...)