各种电话状态差异化



如何区分来电和去电? 对于这两种情况,手机都处于摘机状态,但对于传入呼叫,它处于振铃状态,然后进入摘机状态,

我尝试了这段代码,但它不能有效地工作,因为振铃变量总是假的。 还有一个问题,我怎样才能得到拨出的电话号码..提前致谢

public class states extends BroadcastReceiver {
boolean Ringing = false;
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)) {
Toast.makeText(context, "Ringing", Toast.LENGTH_SHORT).show();
Ringing = true ;
}
else if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
if (Ringing) {
// do nothing
}else{
// do something
}
else if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)) {
Ringing = false ;
}
}
}

这些是我的权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

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

为了区分传入和传出呼叫,您可以使用广播接收器: 在接收器中添加<action android:name="android.intent.action.NEW_OUTGOING_CALL" />并在 onReceive(( 方法中处理它。

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

用于获取传出号码

public void onReceive(Context context, Intent intent)
{
String state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state==null)
{
//Outgoing call
String number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("tag","Outgoing number : "+number);
}
else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
//Incoming call
String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i("tag","Incoming number : "+number);
}
}

相关内容

  • 没有找到相关文章

最新更新