【问题标题】:Reject call number in Android在Android中拒绝电话号码
【发布时间】:2016-09-09 13:31:40
【问题描述】:

我想在Android中编写一些可以检测电话号码和状态的代码。

因此,当某个特定号码(例如 123456789)正在呼叫时,应用会拒绝该号码并结束通话。我该怎么办?

public class ReciveCalls extends BroadcastReceiver {
    public  void onReceive(Context context, Intent intent){
        if(intent.getAction().equals("android.intent.action.PHONE_STATE")){
            String state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            String Number=intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
            Toast toast=Toast.makeText(context,"Number:"+Number+",state:"+state,Toast.LENGTH_LONG);
            toast.setGravity(Gravity.TOP|Gravity.LEFT,3,0);
            toast.show();
        }
      }
}

清单:

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

【问题讨论】:

    标签: android telephony phone-call


    【解决方案1】:

    您可以使用此代码:

    public void onReceive(Context context, Intent intent) {
        // Get AudioManager
        this.context = context;
        // Get TelephonyManager
        telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
            if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                // Get incoming number
                incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                if(incomingNumber) //check the number and reject the call
                {
                        rejectCall();
                }
    
    
            }
        }
    
    
    }
    

    并使用以下方法拒接来电:

     private void rejectCall() {
        try {
    
            // Get the getITelephony() method
            Class<?> classTelephony = Class.forName(telephonyManager.getClass().getName());
            Method method = classTelephony.getDeclaredMethod("getITelephony");
            // Disable access check
            method.setAccessible(true);
            // Invoke getITelephony() to get the ITelephony interface
            Object telephonyInterface = method.invoke(telephonyManager);
            // Get the endCall method from ITelephony
            Class<?> telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName());
            Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");
            // Invoke endCall()
            methodEndCall.invoke(telephonyInterface);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-23
      • 1970-01-01
      相关资源
      最近更新 更多