SoFunction
Updated on 2025-04-10

Android implements blacklisting function based on AudioManager and PhoneStateListener

This article describes the blacklisting function based on AudioManager and PhoneStateListener. Share it for your reference, as follows:

There is generally a function of setting a blacklist on your mobile phone. This example uses the phone blacklist to set up. When the phone on the blacklist calls, the phone ringtone will be muted.

The program code is as follows:

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class A11Activity extends Activity {
 private EditText et;//Add input box, enter and set blacklist private TextView tv01,tv02; //tv01 is used to display the blacklist entered in EditText; tv02 is used to display the current status of the mobile phone.  /** Called when the activity is first created. */
  @SuppressWarnings("static-access")
 @Override
  public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView();
    tv01=(TextView)findViewById(.tv01);
    tv02=(TextView)findViewById(.tv02);
    et=(EditText)findViewById();
    /**Create an object of PhoneCallListener; where PhoneCallListener is
      * Extended the PhoneStateListener class*/
    PhoneCallListener pcl=new PhoneCallListener();
    TelephonyManager tm=(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
    //Set the function of monitoring phone    (pcl, pcl.LISTEN_CALL_STATE);
  }
  public class PhoneCallListener extends PhoneStateListener{
   public void onCallStateChanged(int state,String incomingNumber){
   switch(state){
   //When the phone status is standby, the phone ringtone is in normal mode   case TelephonyManager.CALL_STATE_IDLE:
    ("CALL_STATE_IDLE");
    AudioManager am=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
    (AudioManager.MODE_NORMAL);
    (AudioManager.STREAM_RING);
    break;
    //The mobile phone status is on call   case TelephonyManager.CALL_STATE_OFFHOOK:
    ("CALL_STATE_OFFHOOK");
    break;
    //When the mobile phone mode is ringing   case TelephonyManager.CALL_STATE_RINGING:
    ("CALL_STATE_RINGING");
    // When the incoming call number is the same as the phone number on the blacklist, set the phone ringtone to mute    if((().toString())){
    AudioManager am01=(AudioManager)getSystemService(AUDIO_SERVICE);
    (AudioManager.RINGER_MODE_SILENT);
    (AudioManager.STREAM_RING);
    (, "The phone on the blacklist is muted!!", Toast.LENGTH_LONG).show();
    }
   }
   (state, incomingNumber);
   (new OnKeyListener(){
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    (());
    ("");
    return true;
    }
    });
   }
  }
}

For more information about Android related content, please check out the topic of this site:Android control usage summary"and"Android development introduction and advanced tutorial

I hope this article will be helpful to everyone's Android programming design.