2016-09-09 6 views
-1

Androidに電話番号と状態を検出できるコードを書きたいと思います。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> 

答えて

2

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

} 
関連する問題