实验7 BindService模拟通信

实验报告

课程名称

基于Android平台移动互联网开发

实验日期

2016年4月22日

实验项目名称

 BindService模拟通信

实验地点

S30010

实验类型

□验证型    √设计型    □综合型

学  时

2

一、实验目的及要求(本实验所涉及并要求掌握的知识点)

1.目的:实现启动端和BindService之间的双向通信

2.要求:实现从启动端传递一个数据至BindService端;

实现使用BindService服务播放项目源文件中的音乐;

实现在启动端通过“增加”和“降低”两个按钮控制音频音量大小。

实现在启动端通过“暂停”按钮控制音频暂停播放。

二、实验环境(本实验所使用的硬件设备和相关软件)

(1)PC机

(2)操作系统:Windows XP

(3)软件: Eclipse, JDK1.6,Android SDK,ADT

三、实验内容及步骤

1)完善Activity类

2)新建Service类

四、实验结果(本实验源程序清单及运行结果或实验结论、实验设计图)

 

代码:

sudoku的musicsetting.xml的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

 

    <TextView

        android:id="@+id/textView1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="音量" />

 

    <SeekBar

        android:id="@+id/seekBar1"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />

 

    <ProgressBar

        android:id="@+id/progressBar1"

        style="?android:attr/progressBarStyleHorizontal"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        />

 

    <CheckBox

        android:id="@+id/checkBox1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="静音" />

 

    <TextView

        android:id="@+id/textView2"

        android:layout_width="wrap_content"

        android:layout_height="0dp"

        android:text="" />

 

</LinearLayout>

sudoku的musicsetting.java部分代码:

public class MusicBService extends Service implements MediaPlayer.OnCompletionListener {

   MediaPlayer player;

   private final IBinder binder=new AudioBinder();

    @Override

    public IBinder onBind(Intent arg0) {

        // TODO Auto-generated method stub

        return binder;

    }

    @Override

    public void onCompletion(MediaPlayer player) {

        // TODO Auto-generated method stub

        stopSelf();

    }

    public void onCreate(){

        super.onCreate();

        player=MediaPlayer.create(this, R.raw.model);

        player.setOnCompletionListener(this);

    }

    public int onStartCommand(Intent intent,int flags,int startId){

        if(!player.isPlaying()){

            player.start();

        }

        return START_STICKY;

    }

    public void onDestroy(){

        if(player.isPlaying()){

            player.stop();

        }

        player.release();

    }

    class AudioBinder extends Binder{

        MusicBService getService(){

            return MusicBService.this;

        }

    }

   

    public boolean onUnbind(Intent intent){

        return super.onUnbind(intent);

    }

 

}

}

运行结果:(截图)

1. sudoku项目效果图:

 

 

 

五、实验总结(对本实验结果进行分析,实验心得体会及改进意见)

通过这一次的实验,我对service类有了一点点的理解,希望可以越做越好。

实验评语

 

实验成绩

 

指导教师签名:              年   月   日

           
posted @ 2016-05-02 15:12  35许燕  阅读(171)  评论(0编辑  收藏  举报