Implementation method of audio mute by judging network state by Android

  • 2021-10-24 23:42:21
  • OfStack

In practical application, we don't want to display a loud sound when opening the game in the classroom network, which will affect the quality of class. Therefore, it is necessary to make app intelligent, so that app can automatically mute and other operations according to the current network status of users.

This content is divided into two parts: 1. Identify the network environment 2. Realize app automatic mute.

Automatic mute


/**
 *  Realize the mute function 
 */
private void silentSwitchOn() {
  AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  if (audioManager != null) {
    audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); // Silent mode 
    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.FLAG_PLAY_SOUND); //  The media volume is set to 0 (Mute) 
  }
}

Where AudioManager.STREAM_MUSIC represents the media volume, it can also be replaced with other types to obtain other types of volume.

The monitor volume key is pressed

Overriding the onKeyDown method in activity


public boolean onKeyDown(int keyCode, KeyEvent event) {
  Log.d(TAG, "onKeyDown" + keyCode + "" +   (keyCode==KeyEvent.KEYCODE_VOLUME_UP));
  if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
  //  Volume + Key 
  }
  if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
  //  Volume - Key 
  }
}

Identify the network environment

When our App makes network requests, it often encounters network disconnection, reconnection, data traffic and WIFI changes, so how do we judge the current situation? Next, we will introduce the commonly used network state judgment methods.

Determine whether there is a network connection Determining whether the WIFI network is available Determine whether data traffic is available Gets the type information of the current network connection Get the current network state Judge whether it is a teaching point network

First, get the network status permission in the registry:


<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

1. Determine if you have a network connection


public static boolean isNetworkConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (mNetworkInfo != null) { return mNetworkInfo.isAvailable(); } } return false; }

Return to true when there is a network and false when there is no network.

1. Determine whether the WIFI network is available


public static boolean isWifiConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mWiFiNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (mWiFiNetworkInfo != null) { return mWiFiNetworkInfo.isAvailable(); } } return false; }

It is the WIFI network that returns true, not WIFI that returns false.

1. Determine whether data traffic is available


public static int getConnectedType(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (mNetworkInfo != null && mNetworkInfo.isAvailable()) { return mNetworkInfo.getType(); } } return -1; }

Returns true when it is data traffic, not false.

Gets the type information of the current network connection


public static int getConnectedType(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); if (mNetworkInfo != null && mNetworkInfo.isAvailable()) { return mNetworkInfo.getType(); } } return -1; }

Get the current network state

No Network: 0 WIFI Network: 1 3G Network: 2 2G Network: 3


public static int getAPNType(Context context) { int netType = 0; ConnectivityManager connMgr = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo == null) { return netType; } int nType = networkInfo.getType(); if (nType == ConnectivityManager.TYPE_WIFI) { netType = 1;// wifi } else if (nType == ConnectivityManager.TYPE_MOBILE) { int nSubType = networkInfo.getSubtype(); TelephonyManager mTelephony = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS && !mTelephony.isNetworkRoaming()) { netType = 2;// 3G } else { netType = 3;// 2G } } return netType; }

1. Determine whether it is a teaching point network


/**
 *  Judge whether it is a teaching point network. If it is a teaching point network, it will be muted by default 
 */
private void enablePlay() {
  String wifiNameSp = (String) CommonUtils.getMySP(this( Context ), "Wi-Fi Name ( String )", "wifiName", String.class, "");
  String wifiName = CommonUtils.getConnectWifiSsid();
  if (!TextUtils.isEmpty(wifiNameSp) && !TextUtils.isEmpty(wifiName)
      && CommonUtils.isWifiRequirements(wifiName, wifiNameSp, true)) {
    //  Default mute 
    silentSwitchOn(); //  Call the mute method at the beginning 
  }
}

Summarize


Related articles: