Experiment 7 Bindservice Analog communication

Source: Internet
Author: User

Lab Report

Course Name

Mobile Internet development based on Android platform

Date of experiment

April 22

Name of the experimental project

Bindservice Analog Communication

Location of the experiment

S3010

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

First, the design of the music settings of the XML layout file

Second, the program to play music after login

Third, in the music design to control the music volume size

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

Code:

mainactivity Key code:

Private int Mmusicvolume;

Private Musicservice.mybinder Binder;

ActionBar ActionBar;

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= (Musicservice.mybinder) arg1;

}

};

Intent intentser=New Intent (mainactivity. this, Musicservice. class);

Bindservice (Intentser, Connection, Service. Bind_auto_create);//Play Music

Case R.id. AI2://with value jump

Item.setchecked (true);

Mmusicvolume=binder.getvolume ();

Intent intent=New Intent ();

Intent.setclass (mainactivity. this, Music_main. class);

Bundle bundle=New bundle ();

Bundle.putint ("MUSICB", Mmusicvolume);

Intent.putextras (bundle);

StartActivity (Intent);

break;

musicservice Key code:

MediaPlayer mp=New MediaPlayer ();

Private Mybinder binder=New mybinder ();

Public class Mybinder extends binder{

Public int Getvolume ()

{

return am.getstreamvolume (AM. Stream_music);

}

}

Public IBinder Onbind (Intent arg0) {

TODO auto-generated Method stub

return Binder;

}

@Override

Public void onCreate () {

Super. OnCreate ();//Initialize MediaPlayer object, ready to play music, music file in folder/res/raw

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

String sdcard=environment. getexternalstoragedirectory (). GetPath ();

Try {

Mp.setdatasource (sdcard+file. Separator+ "Bjmusic.mp3");

} catch (IllegalArgumentException e) {

TODO auto-generated Catch block

E.printstacktrace ();

} catch (SecurityException e) {

TODO auto-generated Catch block

E.printstacktrace ();

} catch (IllegalStateException e) {

TODO auto-generated Catch block

E.printstacktrace ();

} catch (IOException e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

Try {

Mp.prepare ();

} catch (IllegalStateException e) {

TODO auto-generated Catch block

E.printstacktrace ();

} catch (IOException e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

Mp.start ();

}

@Override

Public void OnDestroy () {

Super. OnDestroy ();

Stop playing music

Mp.stop ();

}

music_main Key code:

Public class Music_main extends activity{

Private RadioButton openmusic,closemusic;

Private Radiogroup Who_group;

MediaPlayer mediaplayer=New MediaPlayer ();

Private SeekBar Musicseekbar;

Private Audiomanager Audiomanager;

Private int Currentvol,maxvol;

@Override

protected void onCreate (Bundle savedinstancestate) {

TODO auto-generated Method stub

Super. OnCreate (savedinstancestate);

Setcontentview (r.layout. Music);

Musicseekbar= (SeekBar) Findviewbyid (r.id. SeekBar1);

Openmusic= (RadioButton) Findviewbyid (r.id. Opmusic);

Closemusic= (RadioButton) Findviewbyid (r.id. Clomusic);

who_group= (Radiogroup) Findviewbyid (r.id. Musicradiogroup);

Bundle bundle=New bundle ();

Bundle=getintent (). Getextras ();

Musicseekbar.setprogress (Bundle.getint ("MUSICB"));

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

Maxvol=audiomanager.getstreammaxvolume (Audiomanager. Stream_music);

Musicseekbar.setmax (Maxvol);

Who_group.setoncheckedchangelistener (new Radiogroup.oncheckedchangelistener () {

@SuppressLint ("Newapi") @Override

Public void oncheckedchanged (radiogroup arg0, int arg1) {

TODO auto-generated Method stub

Intent Intent=new Intent (Music_main. this, Musicservice. class);

if (Arg1==r.id. Opmusic) {

}Else if(arg1==r.id. Clomusic) {

currentvol=0;

Musicseekbar.setprogress (Currentvol);

Audiomanager.setstreamvolume (audiomanager.stream_music, 0, 0);

}

}

});

Musicseekbar.setonseekbarchangelistener (new Onseekbarchangelistener () {

@Override

Public void Onstoptrackingtouch (SeekBar arg0) {

TODO auto-generated Method stub

}

@Override

Public void Onstarttrackingtouch (SeekBar arg0) {

TODO auto-generated Method stub

}

@Override

Public void onprogresschanged (SeekBar arg0, int aarg1, boolean arg2) {

TODO auto-generated Method stub

Currentvol=audiomanager.getstreamvolume (Audiomanager. Stream_music);

Musicseekbar.setprogress (Currentvol);

Audiomanager.setstreamvolume (Audiomanager. Stream_music, AARG1, 0);

}

});

}

}

Run Result: ()

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

The experiment is difficult, the code is difficult to understand, there are many details of the place want to achieve, but it takes a little time, such as Seekbar only point to open the background music to slide, point off can not slide, and do not know why click to close the background music Seekbar can not jump back to the initial 0 there (the actual is already 0 ), it takes a little bit to jump back.

Experimental reviews

Experimental results

Guide Teacher Signature: Date

Experiment 7 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.