我是新用户。我想在我的应用程序中进行呼叫转发功能。 假设我拨打X号码然后自动转发到Y号码。在这里,我编写了我测试过的代码。但它不起作用。请帮帮我..我今晚需要它...请
public class PhoneStateReceiver extends BroadcastReceiver
{
private Context mContext;
private boolean isRinging = false;
private boolean callReceived = false;
TelephonyManager telephonyManager;
public static CustomPhoneStateListener customPhoneListener;
private String default_number = "x";
private String callForwardNumber = "y";
@Override
public void onReceive(Context context, Intent intent)
{
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
customPhoneListener = new CustomPhoneStateListener();
telephonyManager.listen(customPhoneListener,PhoneStateListener.LISTEN_CALL_STATE
| PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR
| PhoneStateListener.LISTEN_SERVICE_STATE);
mContext = context;
if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL))
{
if (getResultData()!=null && getResultData().equalsIgnoreCase(default_number))
{
String url = "tel:"+"**21*"+ callForwardNumber+Uri.encode("#");
Intent intent1 = (new Intent(Intent.ACTION_CALL, Uri.parse(url)));
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent1);
// setResultData(callForwardNumber);
}
}
}
private class CustomPhoneStateListener extends PhoneStateListener
{
@Override
public void onCallForwardingIndicatorChanged(boolean cfi) {
Log.e("","onCallForwardingIndicatorChanged CFI ="+cfi);
super.onCallForwardingIndicatorChanged(cfi);
}
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
switch (state)
{
case TelephonyManager.CALL_STATE_RINGING:
// Toast.makeText(mContext, "It was a ringing call from "+incomingNumber,Toast.LENGTH_SHORT).show();
isRinging = true;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// Toast.makeText(mContext, "It was a offhook call from "+incomingNumber,Toast.LENGTH_SHORT).show();
callReceived = true;
break;
case TelephonyManager.CALL_STATE_IDLE:
if(isRinging == true && callReceived == false)
{
// Toast.makeText(mContext, "It was a missed call from "+incomingNumber,Toast.LENGTH_SHORT).show();
// Intent intent = new Intent(mContext, AddActivity.class);
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// mContext.startActivity(intent);
telephonyManager.listen(null, PhoneStateListener.LISTEN_NONE);
}
else if(isRinging == true && callReceived == true)
{
// Toast.makeText(mContext, "Call Received.",Toast.LENGTH_SHORT).show();
}
else if(isRinging == false && callReceived == true)
{
// Toast.makeText(mContext, "Call Ended.",Toast.LENGTH_SHORT).show();
// Intent intent = new Intent(mContext, AddActivity.class);
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// mContext.startActivity(intent);
telephonyManager.listen(null, PhoneStateListener.LISTEN_NONE);
}
break;
default:
break;
}
}
}
}