实验七

一、实验目的及要求

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项目效果图:

五、实验总结

通过这次试验,我对安卓编程有了全新的认识。让我对安卓软件功能的设置有了更深的了解。

posted @ 2016-05-03 11:29  44周博为  阅读(116)  评论(0编辑  收藏  举报