标签:
课程名称 |
基于Android平台移动互联网开发 |
实验日期 |
2016.4.5 |
||
实验项目名称 |
BindService模拟通信 |
实验地点 |
S3010 |
||
实验类型 |
□验证型 √设计型 □综合型 |
学 时 |
2 |
||
一、实验目的及要求(本实验所涉及并要求掌握的知识点) |
|||||
|
|||||
二、实验环境(本实验所使用的硬件设备和相关软件) |
|||||
(1)PC机 (2)操作系统:Windows XP (3)软件: Eclipse, JDK1.6,Android SDK,ADT |
|||||
三、实验内容及步骤 |
|||||
|
|||||
四、实验结果(本实验源程序清单及运行结果或实验结论、实验设计图) |
|||||
代码: 1、musicsetting.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/textView1" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="音量" /> 12 13 <SeekBar 14 android:id="@+id/seekBar1" 15 android:layout_width="match_parent" 16 android:layout_height="wrap_content" /> 17 18 <CheckBox 19 android:id="@+id/checkBox1" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:text="静音" /> 23 24 <TextView 25 android:id="@+id/textView2" 26 android:layout_width="fill_parent" 27 android:layout_height="wrap_content" 28 android:layout_marginTop="180dp" 29 android:text="" /> 30 31 </LinearLayout> 2、MusicSetting.java 1 package com.example.sukodu; 2 3 import android.app.Activity; 4 import android.content.Context; 5 import android.media.AudioManager; 6 import android.os.Bundle; 7 import android.widget.CheckBox; 8 import android.widget.CompoundButton; 9 import android.widget.CompoundButton.OnCheckedChangeListener; 10 import android.widget.SeekBar; 11 import android.widget.SeekBar.OnSeekBarChangeListener; 12 import android.widget.TextView; 13 14 public class MusicSetting extends Activity{ 15 private SeekBar musicBar; 16 public AudioManager audiomg; 17 private CheckBox ckbox; 18 private TextView tv2; 19 private int max,current,maxVolume,currentVolume; 20 @Override 21 22 protected void onCreate(Bundle savedInstanceState) { 23 // TODO Auto-generated method stub 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.musicsetting); 26 musicBar = (SeekBar) findViewById(R.id.seekBar1); 27 ckbox = (CheckBox) findViewById(R.id.checkBox1); 28 tv2 = (TextView) findViewById(R.id.textView2); 29 Bundle bundle=new Bundle(); 30 bundle=getIntent().getExtras(); 31 max = bundle.getInt("max"); 32 current = bundle.getInt("current"); 33 musicBar.setMax(max); 34 musicBar.setProgress(current); 35 maxVolume = max; 36 currentVolume = current; 37 audiomg = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 38 musicBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 39 40 @Override 41 public void onStopTrackingTouch(SeekBar arg0) { 42 // TODO 自动生成的方法存根 43 44 } 45 46 @Override 47 public void onStartTrackingTouch(SeekBar arg0) { 48 // TODO 自动生成的方法存根 49 50 } 51 52 @Override 53 public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) { 54 // TODO 自动生成的方法存根 55 if (ckbox.isChecked()) { 56 currentVolume = 0; 57 musicBar.setProgress(currentVolume); 58 audiomg.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0); 59 }else { 60 currentVolume = audiomg.getStreamVolume(AudioManager.STREAM_MUSIC); 61 musicBar.setProgress(currentVolume); 62 audiomg.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0); 63 } 64 tv2.setText("CurrentSS:"+current+";"+"MaxSS:"+max+";"+"CurrentMS:"+currentVolume+";"+"MaxMS:"+maxVolume); 65 66 } 67 }); 68 ckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 69 70 @Override 71 public void onCheckedChanged(CompoundButton arg0, boolean arg1) { 72 // TODO 自动生成的方法存根 73 currentVolume = 0; 74 musicBar.setProgress(currentVolume); 75 tv2.setText("CurrentSS:"+current+";"+"MaxSS:"+max+";"+"CurrentMS:"+currentVolume+";"+"MaxMS:"+maxVolume); 76 77 } 78 }); 79 } 80 } 3、MainActivity.java 1 package com.example.sukodu; 2 3 import android.app.Activity; 4 import android.app.Service; 5 import android.content.ComponentName; 6 import android.content.Context; 7 import android.content.Intent; 8 import android.content.ServiceConnection; 9 import android.media.AudioManager; 10 import android.os.Bundle; 11 import android.os.IBinder; 12 import android.util.Log; 13 import android.view.Menu; 14 import android.view.MenuItem; 15 import android.view.View; 16 import android.view.View.OnClickListener; 17 import android.widget.Button; 18 import android.widget.TextView; 19 20 public class MainActivity extends Activity { 21 private Button exitbtn; 22 private TextView tv1; 23 private BServiceMusic service; 24 // private BServiceMusic.MyBinder binder; 25 private int maxSS,currentSS,maxMS,currentMS; 26 private ServiceConnection connection=new ServiceConnection() { 27 28 @Override 29 public void onServiceDisconnected(ComponentName arg0) { 30 // TODO Auto-generated method stub 31 32 } 33 34 @Override 35 public void onServiceConnected(ComponentName arg0, IBinder arg1) { 36 // TODO Auto-generated method stub 37 // binder=(BServiceMusic.MyBinder)arg1; 38 service = ((BServiceMusic.AudioBinder) arg1).getService(); 39 } 40 }; 41 @Override 42 protected void onCreate(Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 setContentView(R.layout.activity_main); 45 46 47 Intent intent1=new Intent(MainActivity.this, BServiceMusic.class); 48 startService(intent1); 49 bindService(intent1, connection, Service.BIND_AUTO_CREATE); 50 AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); 51 maxSS = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM); 52 currentSS = mAudioManager.getStreamVolume(AudioManager.STREAM_SYSTEM); 53 maxMS = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); 54 currentMS = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); 55 tv1.setText("CurrentSS:"+currentSS+";"+"MaxSS:"+maxSS+";"+"CurrentMS:"+currentMS+";"+"MaxMS:"+maxMS); 56 // Bundle bundle=new Bundle(); 57 final Intent intent=getIntent(); //获取Intent对象 58 Bundle bundle=intent.getExtras(); //获取传递的数据包 59 // tv1.setText("你的名字是:"+bundle.getString("userName")); 60 61 setContentView(R.layout.activity_main); 62 exitbtn=(Button)findViewById(R.id.exit_btn); 63 tv1=(TextView)findViewById(R.id.textView1); 64 Log.i("test", "333333"); 65 exitbtn.setOnClickListener(new OnClickListener() { 66 67 @Override 68 public void onClick(View arg0) { 69 // TODO Auto-generated method stub 70 71 startActivity(intent); 72 setResult(0x1717,intent); 73 Log.i("test", "444444"); 74 75 finish(); 76 } 77 }); 78 79 } 80 81 @Override 82 public boolean onCreateOptionsMenu(Menu menu) { 83 // Inflate the menu; this adds items to the action bar if it is present. 84 getMenuInflater().inflate(R.menu.main, menu); 85 return true; 86 } 87 @Override 88 public boolean onOptionsItemSelected(MenuItem item) { 89 // TODO Auto-generated method stub 90 91 switch (item.getItemId()) { 92 case R.id.item1: 93 // musicVolume=binder.getVolume(); 94 Intent intent=new Intent(MainActivity.this, MusicSetting.class); 95 Bundle bundle=new Bundle(); 96 bundle.putInt("max", maxMS); 97 bundle.putInt("current", currentMS); 98 intent.putExtras(bundle); 99 startActivity(intent); 100 finish(); 101 break; 102 103 default: 104 break; 105 } 106 return super.onOptionsItemSelected(item); 107 } 108 protected void onDestroy() { 109 super.onDestroy(); 110 unbindService(connection); 111 } 112 }
运行结果:(截图)
|
|||||
五、 实验总结(对本实验结果进行分析,实验心得体会及改进意见) |
|||||
每当运行程序后点击登录就会崩溃,导致实验结果不成功。也不知道该怎么解决,没办法顺利完成。 |
|||||
实验评语 |
|
||||
实验成绩 |
|
指导教师签名: 年 月 日 |
|||
标签:
原文地址:http://www.cnblogs.com/zhushengjie/p/5453557.html