中断在Android中的来电并显示我的自定义用户界面

时间:2014-11-27 14:22:18

标签: android phone-call

如何侵入incomin调用并显示自定义ui。 我正在尝试我的代码,但默认拨号器每次都打开。 下面是我的代码 -

public class CallReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {

                Log.d("CallReceiver","IncomingBroadcastReceiver: onReceive: ");

                String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
                Log.d("CallReceiver","IncomingBroadcastReceiver: onReceive: " + state);
                if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)){

                    Intent i = new Intent(context, IncomingCallActivity.class);
                    i.putExtras(intent);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(i);
                }
}

如何解决这个问题。请帮帮我。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

对于处理程序来电,最好使用CallStateListener

 private class CallStateListener extends PhoneStateListener {
  @Override
  public void onCallStateChanged(int state, String incomingNumber) {
      switch (state) {
          case TelephonyManager.CALL_STATE_RINGING:
           Intent i = new Intent(context, IncomingCallActivity.class);
                    i.putExtras(intent);
                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(i);
          break;
      }
  }

}

这需要android.permission.READ_PHONE_STATE权限并在onCreate()中初始化你的监听器

CallStateListener phoneListener = new CallStateListener();
    TelephonyManager telephonyManager = (TelephonyManager) this
            .getSystemService(Context.TELEPHONY_SERVICE);
    telephonyManager.listen(phoneListener,
            PhoneStateListener.LISTEN_CALL_STATE);