Android controls the size of the phone volume to toggle the sound mode

Source: Internet
Author: User

(1) Procedure description

In the Android API Audiomanager, there is a way to adjust the volume of your phone.

Audioma.adjustvolume (audiomanager.adjust_lower, 0);
Audioma.adjustvolume (audiomanager.adjust_raise, 0);
You can also adjust the phone's sound mode 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

<?xml version= "1.0" encoding= "Utf-8"? ><absolutelayout android:id= "@+id/layout1" android:layout_width= " Fill_parent "android:layout_height=" fill_parent "android:background=" @drawable/white "xmlns:android="/HTTP/ Schemas.android.com/apk/res/android "> <textview android:id=" @+id/mytext1 "android:layout_width=" Wrap_ Content "android:layout_height=" wrap_content "android:text=" @string/str_text1 "android:textsize=" 16sp "Android     : textcolor= "@drawable/black" android:layout_x= "20px" android:layout_y= "42px" > </TextView> <imageview    Android:id= "@+id/myimage" android:layout_width= "48px" android:layout_height= "48px" android:layout_x= "110px" android:layout_y= "32px" > </ImageView> <textview android:id= "@+id/mytext2" android:layout_width= "WR Ap_content "android:layout_height=" wrap_content "android:text=" @string/str_text2 "android:textsize=" 16sp "Android Oid:textcolor= "@drawable/black" android:layout_x="20px" android:layout_y= "102px" > </TextView> <progressbar android:id= "@+id/myprogress" style= "? an Droid:attr/progressbarstylehorizontal "android:layout_width=" 160dip "android:layout_height=" Wrap_content "Android : max= "7" android:progress= "5" android:layout_x= "110px" android:layout_y= "102px" > </ProgressBar> < ImageButton android:id= "@+id/downbutton" android:layout_width= "100px" android:layout_height= "100px" Android:la    yout_x= "50px" android:layout_y= "162px" android:src= "@drawable/down" > </ImageButton> <imagebutton    Android:id= "@+id/upbutton" android:layout_width= "100px" android:layout_height= "100px" android:layout_x= "150px" Android:layout_y= "162px" android:src= "@drawable/up" > </ImageButton> <imagebutton android:id= "@+id/n Ormalbutton "android:layout_width=" 60px "android:layout_height=" 60px "android:layout_x=" 50px "android:layout_y = "272px" android:src="@drawable/normal" > </ImageButton> <imagebutton android:id= "@+id/mutebutton" android:layout_width= "6 0px "android:layout_height=" 60px "android:layout_x=" 120px "android:layout_y=" 272px "android:src=" @drawable/mut E "> </ImageButton> <imagebutton android:id=" @+id/vibratebutton "android:layout_width=" 60px "Andro  id:layout_height= "60px" android:layout_x= "190px" android:layout_y= "272px" android:src= "@drawable/vibrate" > </ImageButton></AbsoluteLayout>

(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);d Ownbutton = (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));}});}


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.