Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to block a mobile number call and message receiving in android application development?

I would like to implement an application for block a mobile number for receiving or sending calls and messages. In my application I am entering mobile number at EditText box then I am clicking a button for block the mobile number which has entered by the user.

I have implemented an activity class as follows:

public class BlockNumberActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ((Button)findViewById(R.id.block)).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String mobileNumer = ((EditText)findViewById(R.id.mobileNum)).getText().toString();
                //How to block entered mobileNumber
            }
        });

        ((Button)findViewById(R.id.unblock)).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                String mobileNumer = ((EditText)findViewById(R.id.mobileNum)).getText().toString();
                //How to unblock entered mobileNumber
            }
        });
    }
}

I think we may use BroadcastReceiver. But I don't have more knowledge on it. Please give me an idea how to implement blocking or unblocking mobile number. Please any body help me.....

like image 583
prasad.gai Avatar asked Mar 28 '12 09:03

prasad.gai


People also ask

How do you block a phone number on an Android?

For Android, go to Settings > Call Settings > Additional Settings > Caller ID. Then, select Hide Number. Your calls will remain anonymous and you can bypass the blocked list.

How do I block someone from calling me without blocking them Android?

Go to the settings app on your smartphone then, tap on 'Sound'. Select the 'Do Not Disturb' option and click on Calls. Once you tap on calls, select the 'Do not allow any calls' from the popup menu and toggle 'allow repeat callers' to off position.


2 Answers

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)

like image 117
5 revs Avatar answered Nov 15 '22 20:11

5 revs


to block incoming call use this http://androidsourcecode.blogspot.in/2010/10/blocking-incoming-call-android.html and also this http://www.codeproject.com/Questions/333253/Block-Incoming-calls-in-android-without-single-rin ,http://www.anddev.org/post52643.html?hilit=incoming%20call#p52643 or to block outgoing see How to Block outgoing calls and Text SMS and Blocking outgoing calls and texts (BroadcastReceiver or Service?) , Android: Taking complete control of phone(kiosk mode), is it possible? How?

like image 40
Ronak Mehta Avatar answered Nov 15 '22 20:11

Ronak Mehta