如何知道我是否在Android的通话?

我想知道我是否在打电话。

如果我正在通话,则启动服务(服务部分已清除)。 我如何做到这一点?

在参加电话会议时,我需要拨打服务…我不知道如何做到这一点? 任何帮助?

你需要广播接收机…

在清单声明广播接收机…

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

另外声明使用权限…

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

广播接收机类…

 package xy; 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 xy; 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 ie no call Toast.makeText(context, "Phone state Idle", Toast.LENGTH_LONG).show(); break; case TelephonyManager.CALL_STATE_OFFHOOK: //when Off hook ie 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; } } } 

TelephonyManager.getCallState()返回CALL_STATE_IDLE,CALL_STATE_OFFHOOK,CALL_STATE_RINGING之一

如果这符合您的要求,那么比Pied Piper更全面的解决scheme更less的代码。

你只能知道来电,但你不能修改。 :(见这2.3为什么2.3版本的Android不hava android.permission.MODIFY_PHONE_STATE?和这是什么解决scheme?