Android Call Blocker

Posted by Unknown on Thursday, October 03, 2013 with No comments
About
            This application is block all incoming calls and send automatic busy message caller mobile.  The automatic message based on user can edit or default busy message. 
Specification
1.      Eclipse IDE
2.      Java 1.6
3.      Android SDK for windows package
4.      Android Smart phone(android 2.3.3 minimum)
Steps
            1.The following steps to use for create an android call blocker application

1.      First create a new Android project

  









12.      Create the following 2 Java class

MainActivity.java



package call.control;
import java.lang.reflect.InvocationTargetException;
import com.android.internal.telephony.ITelephony;
import java.lang.reflect.Method;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.os.RemoteException;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class MainActivity extends Activity{
ToggleButton blockAll_cb;
Button b1,b2;
BroadcastReceiver CallBlocker;
TelephonyManager telephonyManager;
ITelephony telephonyService;
EditText t1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t1=(EditText)findViewById(R.id.T1);
b1=(Button)findViewById(R.id.b1);
b2=(Button)findViewById(R.id.b2);  
b1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {                     
t1.setText("");
}});
b2.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {                     
finish();
}});
initviews();
blockAll_cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
blockAll_cb.setTextColor(Color.RED);
CallBlocker =new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {                            
telephonyManager = TelephonyManager)context.getSystemService
(Context.TELEPHONY_SERVICE);
Class c = null;
try{
c = Class.forName(telephonyManager.getClass().getName());
}
catch (ClassNotFoundException e) {                                     
e.printStackTrace();
}
Method m = null;
try{
      m = c.getDeclaredMethod("getITelephony");
}
catch (SecurityException e)   {                                        
e.printStackTrace();
}
catch (NoSuchMethodException e) {                                
e.printStackTrace();
}
m.setAccessible(true);
try {
telephonyService = (ITelephony)m.invoke(telephonyManager);
}
catch (IllegalArgumentException e) {                             
e.printStackTrace();
}
catch (IllegalAccessException e) {                                     
e.printStackTrace();
}
catch (InvocationTargetException e) {                                        
e.printStackTrace();
}
telephonyManager.listen(callBlockListener, PhoneStateListener.LISTEN_CALL_STATE);
}
PhoneStateListener callBlockListener = new PhoneStateListener(){       
public void onCallStateChanged(int state, String incomingNumber){
if(state==TelephonyManager.CALL_STATE_RINGING){
if(blockAll_cb.isChecked()){
try{       
String s=t1.getEditableText().toString();
telephonyService.endCall();   Toast.makeText(getBaseContext(),    "Incoming Call No : "+incomingNumber,Toast.LENGTH_LONG).show();
String s1="Busy Now Call You Later";                      
if(s.length()>0){
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(incomingNumber, null,  s, null, null);
}
else{
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(incomingNumber, null,  s1, null, null);
}}
catch (RemoteException e) {                                                        
e.printStackTrace();
}}}}};};
IntentFilter filter= new IntentFilter("android.intent.action.PHONE_STATE");
registerReceiver(CallBlocker, filter);
}});}
public void initviews(){
blockAll_cb=(ToggleButton)findViewById(R.id.cbBlockAll);   
blockAll_cb.setTextColor(Color.RED);           
}
@Override
protected void onDestroy() {       
super.onDestroy();
if (CallBlocker != null){
unregisterReceiver(CallBlocker);
CallBlocker = null;
}}}



MyStartupIntentReceiver.java

package call.control;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MyStartupIntentReceiver extends BroadcastReceiver
{
       @Override
     public void onReceive(Context context, Intent intent)
       {
             Intent i = new Intent(context, MainActivity.class); 
         i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(i); 
     }
}

3.     Create the  2 layouts

Main.xml


________________________________________________________________________________

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:background="@drawable/frame"

    android:orientation="vertical"

    android:padding="20px" >

 <TextView        

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text=""

        android:textAppearance="?android:attr/textAppearanceMedium" />    

    <LinearLayout

        android:id="@+id/linearLayout1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:background="@drawable/frame"

        android:padding="20px" >

        <ToggleButton

            android:id="@+id/cbBlockAll"

            android:layout_width="wrap_content"

            android:layout_height="66dp"

            android:layout_weight="0.01"

            android:background="@drawable/frame"

            android:padding="16px"

            android:textOff="OFF"

            android:textOn="ON"

            android:textStyle="bold" >

        </ToggleButton>

        <TextView

            android:id="@+id/textView2"

            android:layout_width="20dp"

            android:layout_height="wrap_content"

            android:text=""

            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView

            android:id="@+id/textView1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:background="@drawable/frame"

            android:padding="16px"

            android:text="Block all Calls"

            android:textColor="#FF0000" />

    </LinearLayout>

    <TextView        

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text=""

        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText

        android:id="@+id/T1"

        android:padding="16px" android:background="@drawable/frame"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_marginBottom="20dip"

        android:layout_marginTop="5dip"

        android:textColor="#ff0000"

        android:singleLine="true" >

    </EditText>

    <LinearLayout

        android:id="@+id/linearLayout2"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_weight="0.06"

        android:gravity="center"

        android:padding="20px" android:background="@drawable/frame" >

        <Button

            android:id="@+id/b1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_marginTop="10dip"

            android:background="@drawable/frame"

            android:padding="16px"

            android:text="Clear"

            android:textColor="#FF0000" >

        </Button>

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:gravity="center"

            android:width="10px" />

        <Button

            android:id="@+id/b2"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:layout_marginTop="10dip"

            android:background="@drawable/frame"

            android:padding="16px"

            android:text="Exit"

            android:textColor="#FF0000" >

        </Button>

    </LinearLayout>

</LinearLayout>

_______________________________________________________________________




Frame.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <gradient android:startColor="#AffF" android:endColor="#Ffff" android:angle="90" />
  <stroke android:color="#0000FF" android:width="2dip" />
  <corners android:radius="10px" />
</shape>

4.      Edit the android manifest file

Androidmanifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      android:versionCode="1"
      android:versionName="1.0" package="call.control">
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <application android:icon="@drawable/icon"                                 android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>          
        <receiver android:enabled="true" android:name=".MyStartupIntentReceiver"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
        <intent-filter>
               <action android:name="android.intent.action.BOOT_COMPLETED" />
               <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
</receiver>
    </application>
    <uses-sdk android:minSdkVersion="3" />
      <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"/>
</manifest>


5.      Sample output




Mob    :       +91-9790766717
                   +91-9500087478
Web    :       www.masterychip.com
Mail     :       projects@masterychip.com


Categories: ,