Java 传出呼叫的Alertdialog不工作。CallBlocker实现失败

Java 传出呼叫的Alertdialog不工作。CallBlocker实现失败,java,android,broadcastreceiver,android-alertdialog,Java,Android,Broadcastreceiver,Android Alertdialog,下面是我的代码,我正在使用它在Outgoing Call期间显示警报。但问题是它不起作用 从 我只需要在我的项目中实现它 提前感谢您的帮助 CallBarring.java public class CallBarring extends BroadcastReceiver { // This String will hold the incoming phone number private String number; CustomDialog dialog;

下面是我的代码,我正在使用它在Outgoing Call期间显示警报。但问题是它不起作用

从 我只需要在我的项目中实现它 提前感谢您的帮助

CallBarring.java

public class CallBarring extends BroadcastReceiver {
    // This String will hold the incoming phone number
    private String number;
    CustomDialog dialog;
    TelephonyManager telephonyManager;
    PhoneStateListener listener;
    Context context;

    @Override
    public void onReceive(final Context context, Intent intent) {
        // If, the received action is not a type of "Phone_State", ignore it
        if (!intent.getAction().equals("android.intent.action.PHONE_STATE"))
            return;

        // Else, try to do some action
        else {
            this.context = context;
            if(dialog == null){
            dialog = new CustomDialog(context);
            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
            dialog.show();
            }
            // Fetch the number of incoming call
            number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            listener = new PhoneStateListener() {
                @Override
                public void onCallStateChanged(int state, String incomingNumber) {
                    String stateString = "N/A";
                    switch (state) {
                    case TelephonyManager.CALL_STATE_IDLE:
                        stateString = "Idle";
                        dialog.dismiss();
                        break;
                    case TelephonyManager.CALL_STATE_OFFHOOK:
                        stateString = "Off Hook";
                        dialog.dismiss();
                        break;
                    case TelephonyManager.CALL_STATE_RINGING:
                        stateString = "Ringing";
                        dialog.show();
                        break;
                    }
                    Toast.makeText(context, stateString,Toast.LENGTH_LONG).show();
                }
            };

            // Register the listener with the telephony manager
            telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

            // Check, whether this is a member of "Black listed" phone numbers
            // stored in the database
            /*if (MainActivity.blockList.contains(new Blacklist(number))) {
                // If yes, invoke the method
                disconnectPhoneItelephony(context);
                return;
            }*/
        }
    }

    // Method to disconnect phone automatically and programmatically
    // Keep this method as it is
    @SuppressWarnings({ "rawtypes", "unchecked" })
    private void disconnectPhoneItelephony(Context context) {
        ITelephony telephonyService;
        TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        try {
            Class c = Class.forName(telephony.getClass().getName());
            Method m = c.getDeclaredMethod("getITelephony");
            m.setAccessible(true);
            telephonyService = (ITelephony) m.invoke(telephony);
            telephonyService.endCall();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
下面是我的Androidmanifest.xml 所有权限似乎都正常


class CustomDialog extends Dialog implements OnClickListener {

    public CustomDialog(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_item);
        Button btnEndCall = (Button) findViewById(R.id.end_call);
        //btnEndCall.set
        btnEndCall.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        disconnectPhoneItelephony(context);
    }
}