Alertdialog на исходящий звонок не работает.Сбой реализации CallBlocker - PullRequest
1 голос
/ 01 апреля 2019

Ниже приведен мой код, который я использую для отображения оповещений во время исходящего звонка.Но проблема в том, что он не работает.

Получено с https://github.com/dharmikvyas/CallBlocker Мне просто нужно реализовать это в моем проекте Заранее спасибо за вашу помощь!

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();
        }
    }
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);
    }
}

ниже мой Androidmanifest.xml Все разрешения в порядке.

  <receiver android:name=".CallBarring" >
        <intent-filter android:priority="100" >
            <action android:name="android.intent.action.PHONE_STATE" />
        </intent-filter>
    </receiver>
</application>

<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
...