Pararth
Pararth

Reputation: 8134

Android; detect if the Vibrate setting in the device is on or off, esp for the case when it rings but does not vibrate

App requirement: To detect when a phone is on Ringing mode with the Vibrate Setting being OFF so the problem comes down to : detect whether the Vibrate Setting is OFF or ON

Relevant information:

To start with, Vibrator has no method like isVibratorModeOn() and the Flags related to Vibrate and EXTRA_VIBRATE_... are all marked deprecated with:

This constant was deprecated in API level 16. Applications should maintain their own vibrate policy based on current ringer mode that can be queried via getRingerMode().

But under getRingerMode(), we won't exactly know if the vibrate setting is off or not with RINGER_MODE_NORMAL;

It will vibrate if the vibrate setting is on.

Now the getVibrateSetting() is also deprecated.

Code which works for detecting the RINGER_MODE on *most devices and o.s. versions including Android M on Nexus 6:

 if(audioManager.getRingerMode()!=AudioManager.RINGER_MODE_SILENT){
            if (audioManager.getStreamVolume(AudioManager.STREAM_RING) != 0) {  

*Exception: Nexus 5 - 5.1.1, probably for all Lollipop versions

I feel its very surprising if this has been missed or i've made the big(gest) silly mistake by missing something obvious. Hopefully, this isn't a waste of your time.

Here's more to explain the problem:
RINGER_MODE_SILENTscenario covered: Scenario Covered - ringer silent mode

RINGER_MODE_VIBRATE scenario covered: Scenario Covered - ringer vibrate mode

DETECTING THIS IS THE PROBLEM:
Problem senario - ringer volume high, vibrate setting off

Screenshot crops from Android M, Nexus 6

Upvotes: 2

Views: 3654

Answers (3)

Top4o
Top4o

Reputation: 674

Parath's solution written in Kotlin with some updates:

private fun shouldVibrateOnCall(): Boolean {
    val audioManager = applicationContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager

    // Ensuring it is not on silent
    if (audioManager.ringerMode != RINGER_MODE_SILENT) {
        // If it is on vibrate mode
        if (audioManager.ringerMode == AudioManager.RINGER_MODE_VIBRATE) {
            return true
            // Else check whether the volume is not 0
        } else if (audioManager.getStreamVolume(AudioManager.STREAM_RING) != 0) {
            if ((1 == Settings.System.getInt(contentResolver, Settings.System.VIBRATE_WHEN_RINGING, 0))) {
                return true
            }
        }
    }
    return false 
}

Hope it saves you some time.

Upvotes: 0

Pararth
Pararth

Reputation: 8134

Update:

the solution is a bit twisted, using a deprecated method in one of the loops, it has worked on all devices i have tested; those of brands; motorola, samsung, htc, lg, xiaomi, micromax, sony

audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
        
if (audioManager.getRingerMode() != AudioManager.RINGER_MODE_SILENT) {
   //ensuring it is not on silent
   if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
      //  if it is on vibrate mode , esp for api 23
   } else if (audioManager.getStreamVolume(AudioManager.STREAM_RING) != 0) {
             //  else check whether the volume is not 0
             if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
             if (audioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER) == AudioManager.VIBRATE_SETTING_ON  
 || audioManager.getVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER) == AudioManager.VIBRATE_SETTING_ONLY_SILENT) { //need to add this to detect a specific scenario in xiaomi device
       // if the device o.s version is not 23 this part works
              }
            }
if ((1 == Settings.System.getInt(ApplicationController.getInstance().getContentResolver(), Settings.System.VIBRATE_WHEN_RINGING, 0))) { 
       // the constant VIBRATE_WHEN_RINGING was added in api 23
       }
}

Upvotes: 4

kajol patel
kajol patel

Reputation: 31

Modified solution to the one given:

Even for API < 23 and API 23 this works fine. It seems fine if you do not use deprecated methods for previous APIs. Tested on Samsung, Motorola, OnePlus, Google Pixel, Google Nexus

AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
                int mode = am.getRingerMode();

            switch (mode) {
                case AudioManager.RINGER_MODE_NORMAL:
                    if ((1 == Settings.System.getInt(getContentResolver(), Settings.System.VIBRATE_WHEN_RINGING, 0))) {
                        // code here
                        Toast.makeText(getApplicationContext(), "=== ring + vibrate mode ===", Toast.LENGTH_LONG).show();
                    } else {
                        // code here
                        Toast.makeText(getApplicationContext(), "=== ring + no vibrate mode ===", Toast.LENGTH_LONG).show();
                    }
                    break;

                case AudioManager.RINGER_MODE_SILENT:
                    // code here
                    Toast.makeText(getApplicationContext(), "=== in silent mode ===", Toast.LENGTH_LONG).show();
                    break;

                case AudioManager.RINGER_MODE_VIBRATE:
                    // code here
                    Toast.makeText(getApplicationContext(), "=== in vibrate mode ===", Toast.LENGTH_LONG).show();
                    break;
            }

Upvotes: 3

Related Questions