Experimental seven Bindservice analog communication

Source: Internet
Author: User

Lab Report

Course Name

Mobile Internet development based on Android platform

Date of experiment

April 22, 2016

Name of the experimental project

Bindservice Analog Communication

Location of the experiment

S30010

Type of experiment

-Verification Type √ design type-comprehensive type

Hours

The purpose and requirements of the experiment (the knowledge points which are involved in this experiment and are required to be mastered)

Enables two-way communication between the start-up and Bindservice.

1) The implementation of a data transfer from the start side to the Bindservice end;

2) realize the use of Bindservice service to play the music in the project source file;

3) enable the audio volume to be controlled by the "add" and "lower" two buttons on the boot side.

4) The implementation of the "Pause" button to control the audio pause playback on the boot side.

second, the experimental environment (the hardware equipment and related software used in this experiment)

(1) PC Machine

(2) Operating system: Windows XP

(3) Software: Eclipse, jdk1.6,android Sdk,adt

Third, the contents and steps of the experiment

1. Create a service in musicsetting using bind mode

2. Create a service Connection listener object

3. Create a class that inherits from the service Bindservice.java and modifies

4. Create an action monitor

5. Define volume control using Audiomanager initialization

6, using the Adjuststreamvolume method to achieve volume size adjustment, Setstreammute method set Mute

Four, the experimental results (the experimental source program list and operating results or experimental conclusions, experimental design drawings)

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:


Five, Experimental summary (analysis of the results of the experiment, experience and improvement of experimental ideas)

At the beginning of the experiment when the idea is not clear, Mainactivity class, Musicsetting class, Bindservice class, Musicservice service between the relationship is very messy, do not know where to create service, Do not know how to create Servic with binding method, do not know where to create a service connection listener object. Some of the parameters of the constructor also do not understand what the meaning, and then carefully read the experimental steps and lesson plans, gradually realize the use of bindservice to adjust the volume size, set mute.

Experimental reviews

Experimental results

Guide Teacher Signature: Date

Experimental seven Bindservice analog communication

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.