课程名称 |
基于Android平台移动互联网开发 |
实验日期 |
2016年5月1日 |
实验项目名称 |
BindService模拟通信 |
实验地点 |
S3012 |
实验类型 |
□验证型 √设计型 □综合型 |
学 时 |
|
一、实验目的及要求(本实验所涉及并要求掌握的知识点) |
|||
【目的】 实现启动端和BindService之间的双向通信 【要求】 1) 实现从启动端传递一个数据至BindService端; 2) 实现使用BindService服务播放项目源文件中的音乐; 3) 实现在启动端通过“增加”和“降低”两个按钮控制音频音量大小。 4) 实现在启动端通过“暂停”按钮控制音频暂停播放。
|
|||
二、实验环境(本实验所使用的硬件设备和相关软件) |
|||
(1)PC机 (2)操作系统:Windows XP (3)软件: Eclipse, JDK1.6,Android SDK,ADT
|
代码如下:
1.Volumeset.java
package com.example.sukodu; import com.example.sukodu.BackmusicService.Mybinder; import android.R.string; import android.annotation.SuppressLint; import android.app.ActionBar; import android.app.Activity; import android.os.Bundle; import android.os.IBinder; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.Toast; public class Volumeset extends Activity { Bundle bd; CheckBox btnsilent; SeekBar sbar; BackmusicService binde; double setting; @SuppressLint("NewApi") public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.musicsetting); bd = getIntent().getExtras(); btnsilent = (CheckBox) findViewById(R.id.checkBox1); int current = bd.getInt("volum"); sbar = (SeekBar) findViewById(R.id.seekBar1); int music = (int) (((double) current / 14) * 100); sbar.setProgress(music); binde = ((Mybinder) bd.getBinder("backmusic")).getService(); Toast.makeText(this, "current volum:" + String.valueOf(music), Toast.LENGTH_LONG).show(); sbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub Toast.makeText(Volumeset.this, "current volum:" + String.valueOf((int) setting), Toast.LENGTH_LONG).show(); } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub btnsilent.setChecked(false); } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub setting = (((double) progress / 100) * 14); binde.setvolum((int) setting); } }); btnsilent.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub binde.setsilent(isChecked); } }); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case android.R.id.home: { finish(); break; } } return super.onOptionsItemSelected(item); } }
2.BackmusicService.java
package com.example.sukodu; import android.app.Service; import android.content.Context; import android.content.Intent; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.Binder; import android.os.IBinder; public class BackmusicService extends Service { MediaPlayer mediaplayer; Mybinder binder=new Mybinder(); AudioManager audioManager; int current; boolean isSilent=false; public class Mybinder extends Binder{ public BackmusicService getService(){ return BackmusicService.this; } } public int getvolum(){ current =audioManager.getStreamVolume(AudioManager.STREAM_MUSIC ); return current; } public void setsilent(boolean isSilent){ if(isSilent){ getvolum(); audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,0,0); }else{ audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,current,0);//tempVolume:音量绝对值 } } public void setvolum(int setting){ audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,setting,0); current=setting; } @Override public IBinder onBind(Intent intent) { // TODO Auto-generated method stub return binder; } @Override public void onCreate() { // TODO Auto-generated method stub mediaplayer = MediaPlayer.create(BackmusicService.this, R.raw.nianlun); mediaplayer.start(); audioManager=(AudioManager) getSystemService(Context.AUDIO_SERVICE); super.onCreate(); mediaplayer.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { // TODO Auto-generated method stub mediaplayer.start(); } }); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // TODO Auto-generated method stub mediaplayer.start(); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); if(mediaplayer.isPlaying()){ mediaplayer.stop(); } } }
3.MainActivity.java
package com.example.sukodu; import com.example.sukodu.BackmusicService.Mybinder; import android.annotation.TargetApi; import android.app.ActionBar; import android.app.Activity; import android.app.Service; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.Build; import android.os.Bundle; import android.os.IBinder; import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; @TargetApi(Build.VERSION_CODES.HONEYCOMB) public class MainActivity extends Activity { private Button exitbtn, aboutbtn, playbtn, chvolBtn; private TextView tv1; private Mybinder binder; BackmusicService bs; Bundle bd; ServiceConnection conn = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { // TODO Auto-generated method stub } @Override public void onServiceConnected(ComponentName name, IBinder service) { // TODO Auto-generated method stub binder = (Mybinder) service; bs = binder.getService(); int volum = bs.getvolum(); Toast.makeText(MainActivity.this, "current volum:" + String.valueOf(volum), Toast.LENGTH_LONG) .show(); } }; @TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bd = getIntent().getExtras(); Intent itmusic = new Intent(); itmusic.setClass(MainActivity.this, BackmusicService.class); bindService(itmusic, conn, Context.BIND_AUTO_CREATE); ActionBar actionBar = getActionBar(); actionBar.show(); ActionBar actionBar1 = getActionBar();// 获取ActionBar对象 actionBar1.setDisplayShowHomeEnabled(true);// 显示应用程序图标 actionBar1.setDisplayHomeAsUpEnabled(true);// 将应用程序图标转变为可点击图标,并添加一个返回箭头。 tv1 = (TextView) findViewById(R.id.textView1); Log.i("test", "333333"); final Intent intent = getIntent(); // 获取Intent对象 Bundle bundle = intent.getExtras(); // 获取传递的数据包 tv1.setText("你的名字是:" + bundle.getString("userName")); aboutbtn = (Button) findViewById(R.id.about_btn); exitbtn = (Button) findViewById(R.id.exit_btn); chvolBtn = (Button) findViewById(R.id.VolBtn); playbtn = (Button) findViewById(R.id.buttonPlayer); playbtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(MainActivity.this, MyPlayer.class); startActivity(intent); } }); exitbtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // startActivity(intent); setResult(0x1717, intent); Log.i("test", "444444"); finish(); } }); aboutbtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(MainActivity.this, SecondActivity.class); startActivity(intent); // finish(); } }); chvolBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(MainActivity.this, Volumeset.class); startActivity(intent); } }); } public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub MenuInflater inflater = new MenuInflater(this); inflater.inflate(R.menu.main, menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { /* * case android.R.id.home: // 创建启动MainActivity的Intent Intent intent = * new Intent(this, MainActivity.class); // * 添加额外的Flag,将Activity栈中处于MainActivity之上的Activity弹出 * intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); * startActivity(intent); break; */ case R.id.help: Intent intent1 = new Intent(this, Help.class); startActivity(intent1); // finish(); break; case R.id.about: Intent intent2 = new Intent(this, About.class); startActivity(intent2); // finish(); break; case R.id.newgame: Intent intent3 = new Intent(this, New_Game.class); startActivity(intent3); // finish(); break; default: break; } return super.onOptionsItemSelected(item); } /* * @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO * Auto-generated method stub getMenuInflater().inflate(R.menu.main, menu); * return true; } * * @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO * Auto-generated method stub if (item.getItemId()==R.id.set) { Intent * intent=new Intent(); * intent.setClass(MainActivity.this,ShuduSettings.class); * startActivity(intent); finish(); * * } return true; } */ }
4.musicsetting.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <SeekBar android:id="@+id/seekBar1" android:layout_width="match_parent" android:layout_height="wrap_content" /> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="静音" /> </LinearLayout>
运行截图:
五、实验总结(对本实验结果进行分析,实验心得体会及改进意见)
这次实验难度很大,耗费了蛮多的时间,遇到的问题有很多,service的相关方法太多有点混淆了。
程序能播放后台音乐,但想调节音量的时候出了点差错,还在改进中。
代码还需继续改进以及完善。