Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android BroadCast receiver for vibrate and silent

I need a broad cast receiver implementation where a user can check whether a phone is going from Ringer mode to silent or silent to vibrate and vice versa.

int volumeValue = (Integer)intent.getExtras().get("android.media.EXTRA_VOLUME_STREAM_VALUE");

I tried this code but it won't me help much because it gives same value from ringer to silent and silent to vibrate and vice versa. I actually want to catch the action when phone go from ringer mode to silent and silent to vibrate Thanks in advance

like image 578
malik M Avatar asked Jan 15 '23 12:01

malik M


1 Answers

Write this code in your onCreate() method of receiver:

 BroadcastReceiver receiver=new BroadcastReceiver(){
          @Override
          public void onReceive(Context context, Intent intent) {
               //code...
          }
      };
      IntentFilter filter=new IntentFilter(
                      AudioManager.RINGER_MODE_CHANGED_ACTION);
      registerReceiver(receiver,filter);

Also add this to mainfest file:

<receiver android:name=".receivers.RingerModeStateChangeReceiver" >
        <intent-filter>
            <action android:name="android.media.RINGER_MODE_CHANGED" />
        </intent-filter>
    </receiver>
like image 188
Name is Nilay Avatar answered Jan 20 '23 16:01

Name is Nilay