简体   繁体   English

Android Exception中的传入呼叫阻止

[英]Incoming Call Block in Android Exception

I am trying to block incoming call in Android using this method but this through this exception.I have tried so many methods but failed to abort incoming call please help what is the problem with this code.I have also added ITelephony interface inside com.android.internal.telephony.ITelephony . 我试图阻止Android中的传入呼叫使用这种方法,但这通过这个例外。我已经尝试了这么多的方法,但未能中止来电请帮助这个代码有什么问题。我还在com.android中添加了ITelephony接口.internal.telephony.ITelephony

Exception 例外

在此输入图像描述 Code

String Messageofenemy;
String Blckno;

public OutgoingReceiver() {
}


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

    try {


        if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)) {


            Intent i = new Intent(context, MyService.class);
            context.startService(i);
            return;
        }


        if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {

            //outgoing call code here

        } else {
            //get the phone state
            String newPhoneState = intent.hasExtra(TelephonyManager.EXTRA_STATE) ?
                    intent.getStringExtra(TelephonyManager.EXTRA_STATE) : null;
            Bundle bundle = intent.getExtras();
            if (newPhoneState != null &&
                    newPhoneState.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                //read the incoming call number
                String phoneNumber =
                        bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);

                db = new DatabaseHelper(context);
                Cursor res = db.getAllrows();
                if (res.getCount() == 0) {

                } else {

                    while (res.moveToNext()) {

                        Blckno = res.getString(0);

                    }


                }
                if (phoneNumber.equals("03352264769")) {

                    try {

                        AudioManager am =
                                (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
                        am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                        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) {
                            Toast.makeText(ctx, "Exception" + String.valueOf(e),
                                    Toast.LENGTH_LONG).show();

                        }


                    } catch (Exception e) {
                        Toast.makeText(context, "Exception is " + String.valueOf(e),
                                Toast.LENGTH_LONG).show();
                    }
                }


            }


        }


    } catch (Exception ex) {

        Toast.makeText(context, "Error is " + String.valueOf(ex),
                Toast.LENGTH_LONG).show();

    }


}


}

Check call state and incoming number on onReceive() . 检查onReceive()上的呼叫状态和来电号码。 And then use disconnectPhoneItelephony(Context context) to abort incoming call - 然后使用disconnectPhoneItelephony(Context context)中止传入呼叫 -

 public class OutgoingReceiver extends BroadcastReceiver { 

        @Override
        public void onReceive(Context context, Intent intent) {      
                 if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                            TelephonyManager.EXTRA_STATE_RINGING)) {
                        if (!intent.getAction().equals("android.intent.action.PHONE_STATE") || intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
                            Toast.makeText(context, "Not phone state", Toast.LENGTH_LONG).show();
                            //return;

                            // Else, try to do some action
                        } else {
                            if (number == null) { //get the incomming call number 
                                number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
             disconnectPhoneItelephony(context); // this is method whick blocks incoming call, this method is provided on the down 
                            }
            }
            }
          }





    // Method to disconnect phone automatically and programmatically
            // Keep this method as it is
            @SuppressWarnings({ "rawtypes", "unchecked" })
            private void disconnectPhoneItelephony(Context context) {
                try {

                    String serviceManagerName = "android.os.ServiceManager";
                    String serviceManagerNativeName = "android.os.ServiceManagerNative";
                    String telephonyName = "com.android.internal.telephony.ITelephony";
                    Class<?> telephonyClass;
                    Class<?> telephonyStubClass;
                    Class<?> serviceManagerClass;
                    Class<?> serviceManagerNativeClass;
                    Method telephonyEndCall;
                    Object telephonyObject;
                    Object serviceManagerObject;
                    telephonyClass = Class.forName(telephonyName);
                    telephonyStubClass = telephonyClass.getClasses()[0];
                    serviceManagerClass = Class.forName(serviceManagerName);
                    serviceManagerNativeClass = Class.forName(serviceManagerNativeName);
                    Method getService = // getDefaults[29];
                            serviceManagerClass.getMethod("getService", String.class);
                    Method tempInterfaceMethod = serviceManagerNativeClass.getMethod("asInterface", IBinder.class);
                    Binder tmpBinder = new Binder();
                    tmpBinder.attachInterface(null, "fake");
                    serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder);
                    IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone");
                    Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class);
                    telephonyObject = serviceMethod.invoke(null, retbinder);
                    telephonyEndCall = telephonyClass.getMethod("endCall");
                    telephonyEndCall.invoke(telephonyObject);

                } catch (Exception e) {
                    e.printStackTrace();
                    Log.d("unable", "msg cant disconect call....");

                }


            } 



        }

Here is interface ITelephony.java - 这是接口 ITelephony.java -

interface ITelephony {

    boolean endCall();

}

And dont forget to add permissions and intent filter on your BroadcastReceiver class on your manifest.xml file - 并且不要忘记在manifest.xml文件上的BroadcastReceiver类上添加权限和意图过滤器 -

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

     <receiver
                android:name=".OutgoingReceiver"
                android:enabled="true">
                <intent-filter android:priority="9999">
                    <action android:name="android.intent.action.PHONE_STATE" />
                    <action android:name="android.intent.action.NEW_OUTGOING_CALL" 
<action android:name="android.intent.action.RESPOND_VIA_MESSAGE"/>

                </intent-filter>
            </receiver>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM