Code: Musicservice Class Code Public class Musicsetting extends Activity { Private TextView TV2; Private Button bindbtn; Private Button getservicebtn; Private Button strongbtn; Private Button lowbtn; Private CheckBox checkbox; Private Serviceconnection connection; protected void onCreate (Bundle savedinstancestate) { Super. OnCreate (savedinstancestate); Setcontentview (r.layout. Music_setting); Final Audiomanager maudiomanager= (Audiomanager) Getsystemservice (Context. Audio_service); Volume Control initialization definition Final Intent Intent=new Intent (musicsetting. this, Bindservice. class); Tv2= (TextView) Findviewbyid (r.id. TextView2); Create a service Connection listener object Connection =new serviceconnection () { Public void onservicedisconnected (componentname arg0) { System. out. println ("Service is disconnected"); } Public void onserviceconnected (componentname arg0, IBinder arg1) { System. out. println ("Service connected"); Create connection Get IBinder } }; Strongbtn= (Button) Findviewbyid (r.id. Strongbtn); Strongbtn.setonclicklistener (new Onclicklistener () { Public void OnClick (View arg0) { int currentvolume = Maudiomanager.getstreamvolume (Audiomanager. Stream_music); Increase volume, bring up the system volume control Maudiomanager.adjuststreamvolume (Audiomanager. Stream_music, Audiomanager. Adjust_raise, Audiomanager. fx_focus_navigation_up); Parameter meaning: sound type, adjust the direction of the volume, optional flag bit Tv2.settext ("Current volume size is:" +currentvolume); } }); Lowbtn= (Button) Findviewbyid (r.id. Lowbtn); Lowbtn.setonclicklistener (new Onclicklistener () { Public void OnClick (View arg0) { Maudiomanager.adjuststreamvolume (Audiomanager. Stream_music, Audiomanager. Adjust_lower, Audiomanager. fx_focus_navigation_up); Parameter meaning: sound type, adjust the direction of the volume, optional flag bit int currentvolume = Maudiomanager.getstreamvolume (Audiomanager. Stream_music); Tv2.settext ("Current volume size is:" +currentvolume); } }); checkbox= (CheckBox) Findviewbyid (r.id. CheckBox1); Checkbox.setonclicklistener (new Onclicklistener () { Public void OnClick (View arg0) { Maudiomanager.setstreammute (Audiomanager. Stream_music,true); } }); } } Bindservice Class Code: Public class Bindservice extends service{ Private int Count; Private Mybinder binder=New mybinder (); Public class Mybinder extends binder{//Create a class Bindservice that inherits from the service, creating an inner class in the Bindservice class } Public void Oncreate () { Super. OnCreate (); System. out. println ("Service is Created"); } Public IBinder Onbind (Intent arg0) { System. out. println ("Service is binded"); return Binder; } } Run Result: () When you click the menu, it displays: Show after clicking Increase Volume
Click Decrease Volume Display:
Tick mute and click the increase volume again to display:
|