2016-09-09 3 views
-1

Ich möchte einen Code in Android schreiben, die Telefonnummer und Status erkennen kann.Ablehnen der Rufnummer in Android

Wenn also eine bestimmte Nummer anruft (sagen wir 123456789), weist die App diese Nummer zurück und beendet den Anruf. Was soll ich machen?

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

Manifest:

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


     } 
    } 


} 

Und Anruf abweisen, indem Sie unten Methode:

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

Antwort

2

Sie diesen Code verwenden können

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

}