在我的应用程序中,当用户拒绝来电时拦截会很好,即选择发送忙音。除了在有来电时得到通知之外,还有什么方法可以做到这一点?我在文档中没有找到任何关于此的内容。此信息是否存储在通话记录中,即我可以轮询通话记录以查看呼叫是否被拒绝或应答?
3 回答
嘿。您可以使用PhoneStateListener
和CallLog
类来查询最近通话列表以查找被拒绝的通话。
http://developer.android.com/reference/android/provider/CallLog.html http://developer.android.com/reference/android/telephony/PhoneStateListener.html
或者,您可以在清单上监视广播,android.intent.action.NEW_OUTGOING_CALL
然后android.intent.action.NEW_INCOMING_CALL
将android.permission.PROCESS_INCOMING_CALLS
其放入android.permission.PROCESS_OUTGOING_CALLS
清单中。
经过半天,搜索和逻辑解决了这个问题,我得到了解决方案:
问题:如果我打电话给一个人,如果我的电话没有被那个人接听,我必须在我身边显示一个 Toast 是我的电话被拒绝
解决方案: 创建一个通过两个动作注册的接收器
android.intent.action.NEW_OUTGOING_CALL android.intent.action.PHONE_STATE
这两个动作都是必要的。
为此,您必须获得许可
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
Manifast.xml
<receiver android:name="Call_Receiver">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</receiver>
还有你的 BroadcastReceiver 类
*Call_Receiver.java*
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
/**
*
*/
public class Call_Receiver extends BroadcastReceiver {
private Context ctx;
@Override
public void onReceive(Context context, Intent intent) {
ctx = context;
OutgoingIncomingCallListener phoneListener = new OutgoingIncomingCallListener();
TelephonyManager telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if (intent.getAction()
.equals("android.intent.action.NEW_OUTGOING_CALL")) {
Log.i("", " :::::::::::: inside onReceive if &&&&&& :::::::::::::");
telephony.listen(phoneListener,
PhoneStateListener.LISTEN_CALL_STATE);
}
}
class OutgoingIncomingCallListener extends PhoneStateListener {
public boolean wasRinging;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
Log.i("", " ************ RINGING ********");
wasRinging = true;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i("", " *********** OFFHOOK ********");
if (!wasRinging) {
// Start your new activity
Log.i("", " *********** if ********");
} else {
// Cancel your old activity
Log.i("", " *********** else ********");
}
// this should be the last piece of code before the break
wasRinging = true;
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.i("", " ::::::::::: IDLE :::::::::");
// this should be the last piece of code before the break
// Log.i("", " *********** OFFHOOK ********");
if (wasRinging) {
Toast.makeText(ctx,
"Your call is disconnected by receiver", 10000)
.show();
}
wasRinging = true;
break;
}
}
}
}
当您不接听电话时,这将向您显示 Toast。
如果使用android.intent.action.NEW_INCOMING_CALL
怎么办?
当您拒绝来电时,callstate
从振铃变为空闲。这意味着你拒绝了。
如果呼叫状态从OFFHOOK
空闲变为空闲,则表示您已接听电话。