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

2

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

1. Achieve two-way communication between the start-up and Bindservice;

2. Implement a data transfer from the start-up to the bindservice end;

3. Realize the use of Bindservice service to play the music in the project source file;

4. The implementation of the "increase" and "reduce" two buttons to control the audio volume at the start-up side;

5. Implement the "Pause" button on the boot side to control the audio pause playback.

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 new project called Musicvolumecontrol;

2. Create a new two Java file named Bservicemusic and musicsetting, and write the relevant code in it;

3. Create a new XML file named Musicsetting and associate it with the Musicsetting.java file to refine the code;

4. Add a pair of tags to the androidmanifest.xml.

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

Code:

In Mainactivity.java:

Public class Mainactivity extends Activity {

Private MediaPlayer mediaplayer=New MediaPlayer ();

Private TextView TView;

Private Bservicemusic.mybinder Binder;

Private int Musicvolume;

Private Button startbtn,pausebtn,exitbtn;

Private Serviceconnection connection=New serviceconnection () {

@Override

Public void onservicedisconnected (componentname arg0) {

TODO auto-generated Method stub

}

@Override

Public void onserviceconnected (componentname arg0, IBinder arg1) {

TODO auto-generated Method stub

Binder= (Bservicemusic.mybinder) arg1;

}

};

@Override

protected void onCreate (Bundle savedinstancestate) {

Super. OnCreate (savedinstancestate);

Setcontentview (r.layout. Activity_main);

tview= (TextView) Findviewbyid (r.id. TextView1);

Startbtn= (Button) Findviewbyid (r.id. Startbtn);

Pausebtn= (Button) Findviewbyid (r.id. Pausebtn);

Exitbtn= (Button) Findviewbyid (r.id. Exitbtn);

Intent intent1=New Intent (mainactivity. this, bservicemusic. class);

Bindservice (Intent1, Connection, Service. Bind_auto_create);

Final Intent Intent=getintent ();

Startbtn.setonclicklistener (new Onclicklistener () {

@Override

Public void OnClick (View arg0) {

TODO auto-generated Method stub

Mediaplayer=mediaplayer. Create (Mainactivity. this, R.raw. nan);

Mediaplayer.start ();

}

});

Pausebtn.setonclicklistener (new Onclicklistener () {

@Override

Public void OnClick (View arg0) {

TODO auto-generated Method stub

if (Mediaplayer.isplaying ()) {

Mediaplayer.pause ();

}Else{

Mediaplayer.start ();

}

}

});

Exitbtn.setonclicklistener (new Onclicklistener () {

@Override

Public void OnClick (View arg0) {

TODO auto-generated Method stub

if (Mediaplayer.isplaying ()) {

Mediaplayer.release ();

}

}

});

}

@Override

protected void OnDestroy () {

TODO auto-generated Method stub

if (Mediaplayer.isplaying ()) {

Mediaplayer.stop ();

}

Mediaplayer.release ();

Super. OnDestroy ();

}

@Override

Public boolean oncreateoptionsmenu (Menu menu) {

Inflate the menu; This adds items to the action bar if it is present.

Getmenuinflater (). Inflate (R.menu. Main, menu);

return true;

}

@Override

Public boolean onoptionsitemselected (MenuItem item) {

TODO auto-generated Method stub

Switch (Item.getitemid ()) {

Case R.id. item1:

Musicvolume=binder.getvolume ();

Intent intent=New Intent (mainactivity. this, musicsetting. class);

Bundle bundle=New bundle ();

Bundle.putint ("Musicvolume", Musicvolume);

Intent.putextras (bundle);

StartActivity (Intent);

Finish ();

break;

default:

break;

}

return Super. onoptionsitemselected (item);

}

}

In Bservicemusic.java:

Public class Bservicemusic extends service{

Private MediaPlayer MediaPlayer;

Private Audiomanager Audiomanager;

Private Mybinder Binder=new mybinder ();

Public class Mybinder extends binder{

Public int Getvolume () {

return audiomanager.getstreamvolume (Audiomanager. Stream_music);

}

}

@Override

Public IBinder Onbind (Intent arg0) {

TODO auto-generated Method stub

return null;

}

@Override

Public void onCreate () {

TODO auto-generated Method stub

Super. OnCreate ();

Audiomanager= (Audiomanager) Getsystemservice (Context. Audio_service);

Mediaplayer=mediaplayer. Create (this, R.raw. nan);

Mediaplayer.start ();

}

}

In Musicsetting.java:

Public class Musicsetting extends activity{

Private ProgressBar Musicbar;

@Override

protected void onCreate (Bundle savedinstancestate) {

TODO auto-generated Method stub

Super. OnCreate (savedinstancestate);

Setcontentview (r.layout. Musicsetting);

Musicbar= (ProgressBar) Findviewbyid (r.id. ProgressBar1);

Bundle bundle=New bundle ();

Bundle=getintent (). Getextras ();

Musicbar.setprogress (Bundle.getint ("Musicvolume"));

}

}

Run Result: ()

Initial interface:

To decrease the volume:

To increase the volume:

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

This experiment is not done on the basis of Sudoku games, but instead of re-creating a project and adding code in it to control the volume changes. The experimental process is difficult, refer to the teacher code to complete.

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.