perfectly blocking incoming calls in android
For my solution, you reject call too late. You can listen callstate like this https://stackoverflow.com/a/15564021/6000796
and https://stackoverflow.com/a/33390453/6000796. On the first ring is CallAudioManager.java
receive oncallstatechanged
and call state is Ring, so you can receive this event, reject call.
case "RINGING":
Log.d(TAG, "State : Ringing, incoming_number : " + incoming_number);
if((previus_state.equals("IDLE")) || (previus_state.equals("FIRST_CALL_RINGING"))){
current_state ="FIRST_CALL_RINGING";
}
How to block a mobile number call and message receiving in android application development?
create PhoneCallReceiver .java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.Toast;
public class PhoneCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
PhoneCallStateListener customPhoneListener = new PhoneCallStateListener(context);
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}}
now create PhoneCallStateListener .java
import java.lang.reflect.Method;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.widget.Toast;
import com.android.internal.telephony.ITelephony;
public class PhoneCallStateListener extends PhoneStateListener {
private Context context;
public PhoneCallStateListener(Context context){
this.context = context;
}
@Override
public void onCallStateChanged(int state, String incomingNumber) {
SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(context);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
String block_number = prefs.getString("block_number", null);
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
//Turn ON the mute
audioManager.setStreamMute(AudioManager.STREAM_RING, true);
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try {
Toast.makeText(context, "in"+block_number, Toast.LENGTH_LONG).show();
Class clazz = Class.forName(telephonyManager.getClass().getName());
Method method = clazz.getDeclaredMethod("getITelephony");
method.setAccessible(true);
ITelephony telephonyService = (ITelephony) method.invoke(telephonyManager);
//Checking incoming call number
System.out.println("Call "+block_number);
if (incomingNumber.equalsIgnoreCase("+91"+block_number)) {
//telephonyService.silenceRinger();//Security exception problem
telephonyService = (ITelephony) method.invoke(telephonyManager);
telephonyService.silenceRinger();
System.out.println(" in "+block_number);
telephonyService.endCall();
}
} catch (Exception e) {
Toast.makeText(context, e.toString(), Toast.LENGTH_LONG).show();
}
//Turn OFF the mute
audioManager.setStreamMute(AudioManager.STREAM_RING, false);
break;
case PhoneStateListener.LISTEN_CALL_STATE:
}
super.onCallStateChanged(state, incomingNumber);
}}
Now in src create this package com.android.internal.telephony
now in this package Right Click -> New -> File now give name ITelephony.aidl
and paste this code
package com.android.internal.telephony;
interface ITelephony {
boolean endCall();
void answerRingingCall();
void silenceRinger();
}
NOTE: Code is tested in Android 2.2 (Froyo),2.3 (GingerBread)
Is there a new, official API for rejecting calls on Android?
It should be quite easy with the new Android P API. Your app needs to request the permission Manifest.permission.ANSWER_PHONE_CALLS
. Then you can call the method endCall
of TelecomManager
class doc here.
Related Topics
Android - How to Receive Broadcast Intents Action_Screen_On/Off
Replace Selector Images Programmatically
How to Listen for Changes in Contact Database
Android Studio - Local Path Doesn't Exist
How to Set Image Button Backgroundimage for Different State
How to Bring an Activity to Foreground (Top of Stack)
Programmatically Select Item Listview in Android
How to Change the Color of Alertdialog Title and the Color of the Line Under It
How to Pass the Values from One Activity to Previous Activity
Counting Chars in Edittext Changed Listener
How to Keep the Screen on in My App
How to Hide Soft Keyboard When Activity Starts
How to Create Jar for Android Library Project
Change Project Name on Android Studio