我正在尝试在启动拨出电话后识别并转移到某个活动。我在Intent过滤器中使用了ACTION_NEW_OUTGOING_CALL
。但是我怎么认识到呼叫是外向的。我为来电做了这个(如下所示),但我可以使用什么而不是EXTRA_STATE_RINGING
。
public class OutgoingBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
Intent i = new Intent(context, OutgoingCallScreenDisplay.class);
i.putExtras(intent);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
答案 0 :(得分:7)
public class OutgoingBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {
// If it is to call (outgoing)
Intent i = new Intent(context, OutgoingCallScreenDisplay.class);
i.putExtras(intent);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
}
ACTION_NEW_OUTGOING_CALL 是Intent
类中的常量声明,而不是TelephonyManager
中的常量声明。当出现呼出呼叫时,系统使用此常量广播意图。如果您真的想使用TelephonyManager
接听拨出电话,请执行以下操作:
TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
PhoneStateListener listener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
// TODO Auto-generated method stub
super.onCallStateChanged(state, incomingNumber);
switch(state) {
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if(incomingNumber==null) {
//outgoing call
} else {
//incoming call
}
break;
case TelephonyManager.CALL_STATE_RINGING:
if(incomingNumber==null) {
//outgoing call
} else {
//incoming call
}
break;
}
}
};
答案 1 :(得分:5)
检测外拨电话事件
<强> 1。创建OutgoingCallBroadcastReceiver
public class OutgoingCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(OutgoingCallReceiver.class.getSimpleName(), intent.toString());
Toast.makeText(context, "Outgoing call catched!", Toast.LENGTH_LONG).show();
//TODO: Handle outgoing call event here
}
}
<强> 2。在AndroidManifest.xml中注册OutgoingCallBroadcastReceiver
<receiver android:name=".OutgoingCallReceiver" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
第3。在AndroidManifest.xml中添加权限
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>