Search code examples
androidandroid-servicecallsystem-callsbackground-service

How to prevent outgoing call in android with a service?


I wanna make a service that when it is active and the user presses the call button, whether in the OS phone app or in the contacts, it prevents outgoing call. I mean instead of running android calling service, i want my service to be run. Is there any way to do so? I am really beginner in android programming and i don't really know a lot about android services. I'll appreciate you to help me. Thanks a lot.


Solution

  • You have to use BroadcastReceiver for this.

    in manifest you have to write

    <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>    
    
    <receiver android:name=".OutGoingCallListener">
           <intent-filter android:priority="0">
               <action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
           </intent-filter>
    
       </receiver>
    

    And in class which extends from BroadcastReceiver you have to write

    public class OutGoingCallListener extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        String number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER).toString();
    
           //if u want to block particular call then check number or else block all call in thid code
    
            setResultData(null);//Canceling call operation 
            Toast.makeText(context, "This call is not Allowed", Toast.LENGTH_LONG).show();
    
    
    } }