DEV Community

How to make a native Android app that can block phone calls

Nikola Brežnjak on February 22, 2018

Originally published on my blog. TL;DR In this post, I'll show you step by step how to make a native Android app that can block certa...
Collapse
 
nikhiljugale007 profile image
Nikhil Subhash Jugale

It is not blocking the call for me..
Th ring continue to ring until i cut the phone.
I am using exact same code from github.
And testing on Android studio emulator pixel 2.

Can anyone please help...

Collapse
 
tmjansson profile image
Mikael Jansson

Just for info.
Works like charm on my Moto G3 runnnig V6.0.1 Android.

But on Moto G7 Power running Android V9, I don't get caller ID and I get:

  • java.lang.SecurityException: MODIFY_PHONE_STATE permission required.
Collapse
 
ishan001 profile image
Ishan Khandelwal

Use TelecomManger to end call programatically for Android 9 and above using ANSWER_PHONE_CALL permission.

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
TelecomManager tm = (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ANSWER_PHONE_CALLS) == PackageManager.PERMISSION_GRANTED) {
success = tm.endCall();
Log.d("call state", "call end");
}
}

Collapse
 
kaushal7171 profile image
kaushal raykar • Edited

hii,its work fine, but it's block the calls even if we close the application

Collapse
 
nikola profile image
Nikola Brežnjak

What are you referring to with 'destroyed'?

Collapse
 
kaushal7171 profile image
kaushal raykar • Edited

i want to de-register the broadcast receiver after press the back button.that is once i exit from app blocking should not be happen.

Thread Thread
 
nikola profile image
Nikola Brežnjak

And what have you tried so far?

Collapse
 
adibacco profile image
adibacco • Edited

It is not working on Motorola G5S plus. What can be wrong? Permissions are granted but incoming call is not blocked. G5S plus is running android 7.1.1 API level 25

Collapse
 
nikola profile image
Nikola Brežnjak

What do you get in the logs when you debug the app?

Collapse
 
adibacco profile image
adibacco

I get a call notification that disappears after less of a second. Sometimes the notification will not pop-up at all. But it is still useful. Thx

Thread Thread
 
nikola profile image
Nikola Brežnjak

This actually means it's working for you.

Collapse
 
surbhivsambare profile image
SurbhiVSambare

I want the calls should get blocked .. how I can achieve that..?

Collapse
 
nikola profile image
Nikola Brežnjak

What have you tried so far?

Collapse
 
surbhivsambare profile image
SurbhiVSambare

Thank you for your response.. I want to block the outgoing calls.. when my application is going in background.. I have written the following code with the help of stackoverflow but the code is not working:-

Will be waiting for your response.. thank you in advance.

public class OutgoingCallBlocking extends BroadcastReceiver {

String number;

@SuppressLint("WrongConstant")
@Override
public void onReceive(Context context, Intent intent) {
    number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
    setResultData(null);
    Toast.makeText(context, "Outgoing Call Blocked "+ number , 5000).show();
}

}

Collapse
 
namnhong profile image
nam giang

Hi Nick, does this work in the background when the app is off?

Thanks

Collapse
 
nikola profile image
Nikola Brežnjak

Yes.

Collapse
 
zoro238 profile image
zoro238

Now this is when there is an incoming call, but when there is an outgoing call and I do not want the outgoing number to appear. How will this be done

Collapse
 
nikola profile image
Nikola Brežnjak

What have you tried so far?

Collapse
 
tejavooh profile image
Teja Vu

Yay! Finally, someone broke the jinx of the code, even I spent a lot of time on StackOverflow, for this, thank you Nikola!

Works like a charm!

Collapse
 
nikola profile image
Nikola Brežnjak

Great, I'm glad this helped you 👍

Collapse
 
itamarmosh profile image
itamarmosh

when I get a call, instead of closing the call my app gets closed. AND the part that says "PERMISSION_REQUEST_READ_PHONE_STATE" is in red. any help?

Collapse
 
ruthvik47 profile image
Ruthvik47

It won't work,it will ask MODIFY_PHONE_STATE permission to block the calls.Now android is not allowing MODIFY_PHONE_STATE permissions to third party applications.

Collapse
 
ishan001 profile image
Ishan Khandelwal

Use TelecomManger to end call programatically for Android 9 and above using ANSWER_PHONE_CALL permission.

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
TelecomManager tm = (TelecomManager) getSystemService(Context.TELECOM_SERVICE);
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ANSWER_PHONE_CALLS) == PackageManager.PERMISSION_GRANTED) {
success = tm.endCall();
Log.d("call state", "call end");
}
}

Collapse
 
bajithelearner profile image
Baji Shaik

Exactly, did you find any solution for this?

Collapse
 
ruthvik47 profile image
Ruthvik47

Nope

Collapse
 
kaushal7171 profile image
kaushal raykar

hello,i use this code but its not working for me,i used oneplus5 phone.in logcat its show ClassCastException for this line= telephonyService = (ITelephony) m.invoke(tm);

Collapse
 
nikola profile image
Nikola Brežnjak

Did you add it through reflection as noted in the post?