Android controls the volume of mobile phones and switches the voice mode

Source: Internet
Author: User

(1) Procedures

Android API's AudioManager provides a method to adjust the volume of mobile phones.

audioMa.adjustVolume(AudioManager.ADJUST_LOWER, 0);
audioMa.adjustVolume(AudioManager.ADJUST_RAISE, 0);
You can also adjust the voice mode of your phone to vibrate or mute.

AudioMa. setRingerMode (AudioManager. RINGER_MODE_NORMAL );

AudioMa. setRingerMode (AudioManager. RINGER_MODE_SILENT );

AudioMa. setRingerMode (AudioManager. RINGER_MODE_VIBRATE );

(2) Layout File

   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
 

(3) code:

package com.liuzuyi.soundmode;import android.app.Activity;import android.content.Context;import android.media.AudioManager;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.ProgressBar;public class MainActivity extends Activity { private ImageView myimage; private ImageButton downbutton; private ImageButton upbutton; private ImageButton normalbutton; private ImageButton mutebutton; private ImageButton vibratebutton; private ProgressBar myprogress; private AudioManager audioMa; private int volume; protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);audioMa =(AudioManager)getSystemService(Context.AUDIO_SERVICE);myimage = (ImageView)findViewById(R.id.myImage);myprogress =(ProgressBar)findViewById(R.id.myProgress);downbutton =(ImageButton)findViewById(R.id.downButton);upbutton =(ImageButton)findViewById(R.id.upButton);normalbutton=(ImageButton)findViewById(R.id.normalButton);mutebutton=(ImageButton)findViewById(R.id.muteButton);vibratebutton=(ImageButton)findViewById(R.id.vibrateButton);volume =audioMa.getStreamVolume(AudioManager.STREAM_RING);myprogress.setProgress(volume);int mode =audioMa.getRingerMode();if(mode == AudioManager.RINGER_MODE_NORMAL ){myimage.setImageDrawable(getResources().getDrawable(R.drawable.normal));}else if(mode == AudioManager.RINGER_MODE_SILENT){myimage.setImageDrawable(getResources().getDrawable(R.drawable.mute));}else if(mode == AudioManager.RINGER_MODE_VIBRATE){myimage.setImageDrawable(getResources().getDrawable(R.drawable.vibrate));}downbutton.setOnClickListener( new OnClickListener() {public void onClick(View v) {audioMa.adjustVolume(AudioManager.ADJUST_LOWER, 0);volume = audioMa.getStreamVolume(AudioManager.STREAM_RING);myprogress.setProgress(volume);int mode =audioMa.getRingerMode();if(mode == AudioManager.RINGER_MODE_NORMAL ){myimage.setImageDrawable(getResources().getDrawable(R.drawable.normal));}else if(mode == AudioManager.RINGER_MODE_SILENT){myimage.setImageDrawable(getResources().getDrawable(R.drawable.mute));}else if(mode == AudioManager.RINGER_MODE_VIBRATE){myimage.setImageDrawable(getResources().getDrawable(R.drawable.vibrate));}}});upbutton.setOnClickListener( new OnClickListener() {public void onClick(View v) {audioMa.adjustVolume(AudioManager.ADJUST_RAISE, 0);volume = audioMa.getStreamVolume(AudioManager.STREAM_RING);myprogress.setProgress(volume);int mode =audioMa.getRingerMode();if(mode == AudioManager.RINGER_MODE_NORMAL ){myimage.setImageDrawable(getResources().getDrawable(R.drawable.normal));}else if(mode == AudioManager.RINGER_MODE_SILENT){myimage.setImageDrawable(getResources().getDrawable(R.drawable.mute));}else if(mode == AudioManager.RINGER_MODE_VIBRATE){myimage.setImageDrawable(getResources().getDrawable(R.drawable.vibrate));}}});normalbutton.setOnClickListener( new OnClickListener() {public void onClick(View v) {audioMa.setRingerMode(AudioManager.RINGER_MODE_NORMAL);    volume = audioMa.getStreamVolume(AudioManager.STREAM_RING);    myprogress.setProgress(volume);myimage.setImageDrawable(getResources().getDrawable(R.drawable.normal));}});mutebutton.setOnClickListener( new OnClickListener() {public void onClick(View v) {audioMa.setRingerMode(AudioManager.RINGER_MODE_SILENT);    volume = audioMa.getStreamVolume(AudioManager.STREAM_RING);    myprogress.setProgress(volume);myimage.setImageDrawable(getResources().getDrawable(R.drawable.mute));}});vibratebutton.setOnClickListener( new OnClickListener() {public void onClick(View v) {audioMa.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);    volume = audioMa.getStreamVolume(AudioManager.STREAM_RING);    myprogress.setProgress(volume);myimage.setImageDrawable(getResources().getDrawable(R.drawable.vibrate));}});}}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.