Blocking Incomming Calls in Android Smart Phone

We want to blocks all incomming calls in android phone.
Create new project and extand it main class from BroadCast Receiver.
public class Blocker extends BroadcastReceiver {
private ITelephony telephonyService;
private String incommingNumber;
private String incommingName=null;
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
Bundle b = intent.getExtras();
String state = b.getString(TelephonyManager.EXTRA_STATE);
if ((state != null)&& (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)))
{
 TelephonyManager telephony = (TelephonyManager)
 context.getSystemService(Context.TELEPHONY_SERVICE);
try
{
Class c = Class.forName(telephony.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
telephonyService = (ITelephony) m.invoke(telephony);
telephonyService.endCall();
} catch (Exception e)
   {
   e.printStackTrace();
  }
}
}

Manifest File

<br /> <manifest android:versioncode="1" android:versionname="1.0" package="com.example.callblockersecond" xmlns:android="http://schemas.android.com/apk/res/android"><br /> <br /> <uses-sdk android:minsdkversion="8" android:targetsdkversion="15"><br /> <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"><br /> <uses-permission android:name="android.permission.CALL_PHONE"><br /> <uses-permission android:name="android.permission.READ_PHONE_STATE"><br /> <uses-permission android:name="android.permission.READ_CONTACTS"><br /> <br /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"><br /> <receiver android:name=".Blocker"><br /> <intent-filter android:priority="1000"><br /> <action android:name="android.intent.action.PHONE_STATE"></action><br /> <action android:name="android.intent.action.NEW_OUTGOING_CALL"><br /> </action></intent-filter><br /> </receiver><br /> </application><br /> <br /> </uses-permission></uses-permission></uses-permission></uses-permission></uses-sdk></manifest>
And the following interface with the given package name. Otherwise it would not work .
package com.android.internal.telephony;

public interface ITelephony {


boolean endCall();
void answerRingingCall();
void silenceRinger();
}
Let enjoy. it will block all your incomming call in android phone.
If you want to block all unsaved numbers than add the following method.
public String getContactDisplayNameByNumber(String number, Context c) {

Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

String name=null;
ContentResolver contentResolver =c.getContentResolver();
Cursor contactLookup = contentResolver.query(uri, new String[] {BaseColumns._ID,
ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);

try {
if (contactLookup != null && contactLookup.getCount() > 0) {
contactLookup.moveToNext();
data = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
//String contactId = contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));
}
} finally {
if (contactLookup != null) {
contactLookup.close();

}
}


return name;
}
In this method we search the calling number in contacts . If it is available than ignor blocking else block it.
Now Add the following number of line to the onReceive( ) method
Bundle b = intent.getExtras();
String state = b.getString(TelephonyManager.EXTRA_STATE);
if ((state != null)&& (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)))
{
incommingNumber = b.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
incommingName=getContactDisplayNameByNumber(incommingNumber, context);

if((incommingName==null)||(incommingName.length()<=1))
{ TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
try
{
  Class c = Class.forName(telephony.getClass().getName());
                  Method m = c.getDeclaredMethod("getITelephony");
                 m.setAccessible(true);
                 telephonyService = (ITelephony) m.invoke(telephony);
                 telephonyService.endCall();
   } catch (Exception e)
          { e.printStackTrace();
          }
      }
}

It block only unsaved numbers.You also block any particular or list of numbers.



                                                        Now I Write Call Blocker Application for android where you can block All, unsaved and customized list number form calling to you. All classes, source code, screen shot and Apk are download load from the below link.     BlockCall Download Here


                                                      Screen Shot of Call Blocker App

                                           





Post a Comment

3 Comments

  1. Hi sir i need code for call blocker please send me. chandbecse@gmail.com

    ReplyDelete
    Replies
    1. Dear !
      Source Code is given at the end of tutorial.Please download it.

      Delete
    2. Files for the source code is missing!!

      Delete