Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

telephony - How to Reject a call programatically in android

In my app I will maintain a list of contacts.

Any calls from contacts in the list will be dropped. They will show under missed calls but the phone will not ring.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First create this Interface:

  public interface ITelephony {

        boolean endCall();

        void answerRingingCall();

        void silenceRinger();

  }

Then Create this class that extends BroadcastReceiver

public class IncomingCallReceiver extends BroadcastReceiver {
    private ITelephony telephonyService;
    private String blacklistednumber = "+458664455";

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

       TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
       try {
         Class c = Class.forName(tm.getClass().getName());
         Method m = c.getDeclaredMethod("getITelephony");
         m.setAccessible(true);
         ITelephony telephonyService = (ITelephony) m.invoke(tm);
         Bundle bundle = intent.getExtras();
         String phoneNumber = bundle.getString("incoming_number");
         Log.e("INCOMING", phoneNumber);
         if ((phoneNumber != null) && phoneNumber.equals(blacklistednumber)) { 
            telephonyService.silenceRinger();
            telephonyService.endCall();
            Log.e("HANG UP", phoneNumber);
         }

       } catch (Exception e) {
         e.printStackTrace();
       }
}

This will only block that single phonenumber, but you get the point.

In your manifest add this:

<receiver android:name=".IncomingCallReceiver">
    <intent-filter android:priority="999">
        <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.PROCESS_INCOMING_CALLS" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...