0

So this link helps a lot: Android mute/unmute phone
But I am a not the best at interpreting other people's code and everytime I try to put in there code my program crashes. Could someone help me with how to I can fix it? Spent about an hour trying to figure it out.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Cooper Scott
  • 651
  • 8
  • 18

1 Answers1

1

All you have to do is in your AndroidManifest.xml add the permission for vibration.

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

After that in your activity whenever you want to change it to vibrate use this code (button press etc.)

AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

To unmute simply use the code

AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);

audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolume, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
Tristan
  • 3,530
  • 3
  • 30
  • 39
  • My Code: *Button 'android:onClick"ClickMe" |||| MainActivity below the onCreate i have: public void ClickMe(Context context) { AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE); audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE); } || App crashes every time the button is exicuted – Cooper Scott Mar 02 '15 at 02:23
  • I cannot see what you are doing there. What should be happening is defining your button (Button mute = findViewById(R.id.yourbutton) Then you should create an onClick event and put the code in there like button.setOnClickListener etc. – Tristan Mar 02 '15 at 02:26
  • Thanks, still learning the Android Development Kit. Problem Solved! – Cooper Scott Mar 02 '15 at 02:34
  • Glad to help! Ask if you need help. – Tristan Mar 02 '15 at 02:35