标签:
课程名称 |
基于Android平台移动互联网开发 |
实验日期 |
5.3 |
||
实验项目名称 |
BindService模拟通信 |
实验地点 |
S3002 |
||
实验类型 |
□验证型 √设计型 □综合型 |
学 时 |
|
||
一、实验目的及要求(本实验所涉及并要求掌握的知识点) |
|||||
实现从启动端传递一个数据至BindService端; 实现使用BindService服务播放项目源文件中的音乐; 实现在启动端通过“增加”和“降低”两个按钮控制音频音量大小。 实现在启动端通过“暂停”按钮控制音频暂停播放。
实现启动端和BindService之间的双向通信.
|
|||||
二、实验环境(本实验所使用的硬件设备和相关软件) |
|||||
(1)PC机 (2)操作系统:Windows XP (3)软件: Eclipse, JDK1.6,Android SDK,ADT |
|||||
三、实验内容及步骤 |
|||||
|
|||||
代码:
PlayMusic代码: package com.example.serviceplaymusic;
import java.text.SimpleDateFormat; import java.util.Date;
import android.app.Activity; import android.app.Service; import android.content.ComponentName; import android.content.Intent; import android.content.ServiceConnection; import android.content.SharedPreferences; import android.os.Binder; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast;
public class PlayMusic extends Activity {
Button start,bind,zj,jx; private MusicService.MyBinder binder; private int com; private int count=0; private boolean star=true; private ServiceConnection conn=new ServiceConnection() {
@Override public void onServiceDisconnected(ComponentName arg0) { // TODO Auto-generated method stub System.out.println("Service is disconnected"); }
@Override public void onServiceConnected(ComponentName arg0, IBinder arg1) { // TODO Auto-generated method stub System.out.println("Service Connected"); binder=(MusicService.MyBinder) arg1; } };
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_play_music); start=(Button)findViewById(R.id.start); zj=(Button)findViewById(R.id.button2); jx=(Button)findViewById(R.id.button3); bind=(Button)findViewById(R.id.button1); final Intent intent=new Intent(PlayMusic.this,MusicService.class); bind.setOnClickListener(new OnClickListener() {
@Override public void onClick(View arg0) { //Object binder;
bindService(intent, conn, Service.BIND_AUTO_CREATE); } }); jx.setOnClickListener(new OnClickListener() {
@Override public void onClick(View arg0) { // TODO Auto-generated method stub binder.setCount(0); com=binder.getCount(); } }); zj.setOnClickListener(new OnClickListener() {
@Override public void onClick(View arg0) { // TODO Auto-generated method stub binder.setCount(1); com=binder.getCount(); } });
start.setOnClickListener(new OnClickListener() {
@Override public void onClick(View arg0) { // TODO Auto-generated method stub binder.setstar(star); star=binder.getstar(); if(star==false) start.setText("暂停"); else start.setText("播放"); } }); }
}MusicService代码: package com.example.serviceplaymusic;
import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Binder; import android.os.IBinder; import android.util.Log; import android.widget.Toast;
public class MusicService extends Service { private MediaPlayer mp; private String TAG="Main"; private SharedPreferences sp; private boolean quit; private boolean star=true; AudioManager mAudioManager;
private int com; private int count; private MyBinder binder=new MyBinder(); public class MyBinder extends Binder{ public void setCount(int count) {
com=count; } public int getCount(){ if(com==1){ mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE, AudioManager.FX_FOCUS_NAVIGATION_UP);
} //增加音量,调出系统音量控制 else if(com==0){ mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER, AudioManager.FX_FOCUS_NAVIGATION_UP); } return com; }
public void setstar(boolean Star){ star=Star; } public boolean getstar(){ if(star==true){ mp.start(); return false; } else { mp.pause(); return true; } } } @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub System.out.println("Service is Binded"); return binder; } @Override public void onCreate() { // TODO Auto-generated method stub super.onCreate(); System.out.println("Service is Created"); new Thread() { public void run() { while(!quit) { try { Thread.sleep(1000); } catch ( InterruptedException e) { // TODO: handle exception } count++; } }; }.start(); mp=MediaPlayer.create(this, R.raw.kzsdqc); mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); } @Override public boolean onUnbind(Intent intent) { // TODO Auto-generated method stub System.out.println("Service is Unbinded"); return super.onUnbind(intent);
} @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); this.quit=true; System.out.println("Service is Destroyed"); }
}
四、实验结果(本实验源程序清单及运行结果或实验结论、实验设计图)
|
|||||
五、实验总结(对本实验结果进行分析,实验心得体会及改进意见) |
|||||
通过实验加深对bindservice服务的理解,运用服务实现对音乐源文件的播放,并实现对该音频音量大小的控制。 |
|||||
实验评语 |
|
||||
实验成绩 |
|
指导教师签名: 年 月 日 |
|||
标签:
原文地址:http://www.cnblogs.com/badgood/p/5454207.html