码迷,mamicode.com
首页 > 其他好文 > 详细

实验7 BindService 模拟通信

时间:2016-05-03 12:41:50      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

实验报告

课程名称

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

实验日期

2015.4.23

实验项目名称

 

实验7 BindService模拟通信

 

实验地点

S30010

实验类型

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

学  时

2

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

1)         实现从启动端传递一个数据至BindService端;

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

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

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

 

 

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

(1)PC机

(2)操作系统:Windows XP

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

三、实验内容及步骤

1)新建工程

 2)修改布局文件main.xml

3)完善Activity类

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

 

代码:

    Main

public class MainActivity extends Activity {

    private BService.MyBinder binder;  

 

   

   

private ServiceConnection conn=new ServiceConnection() {

        @Override

        public void onServiceDisconnected(ComponentName arg0) {

            // TODO Auto-generated method stub

            System.out.println("Service is disconnected");

        }

 

        public void onServiceConnected(ComponentName arg0, IBinder service) {

            // TODO Auto-generated method stub

            System.out.println("Service Connected");

            binder = (MyBinder)service;

            BService bs = binder.getService();

            int volum=bs.getvolum();

 

        }

    };

   

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.music);

         Bundle bundle = getIntent().getExtras();

            Intent itmusic=new Intent();

            itmusic.setClass(MainActivity.this, BService.class);

            bindService(itmusic, conn, Context.BIND_AUTO_CREATE);

          

           

          

music

public class Music extends Activity{

    private BService binder;

    private SeekBar seekBar;

    double setting;

    private Bundle bundle;

    private CheckBox btnsilent;

     AudioManager audioManager;

     private MediaPlayer player=new MediaPlayer();

    @SuppressLint("NewApi")

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        setContentView(R.layout.music);

        bundle =getIntent().getExtras();

        btnsilent=(CheckBox)findViewById(R.id.checkBox1);

        int current =bundle.getInt("current");

        seekBar=(SeekBar) findViewById(R.id.seekBar1);

         Button btn=(Button)findViewById(R.id.button1);

       

       

        int music=(int) (((double)current/14)*100);

         seekBar.setProgress(music); 

         binder=((MyBinder)bundle.getBinder("backmusic")).getService();

           

            Toast.makeText(Music.this, "current volum:"+String.valueOf(music), Toast.LENGTH_LONG).show();

 

          seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

           

            public void onStopTrackingTouch(SeekBar arg0) {

                // TODO Auto-generated method stub

                Toast.makeText(Music.this, "current volum:"+String.valueOf((int) setting), Toast.LENGTH_LONG).show();

            }

           

            public void onStartTrackingTouch(SeekBar arg0) {

                // TODO Auto-generated method stub

 

            }

           

            public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {

                // TODO Auto-generated method stub

               

                setting=(progress/100)*14;

                binder.setvolum((int)setting);

            }

 

           

        });

          btnsilent.setOnCheckedChangeListener(new OnCheckedChangeListener() {

           

            @Override

            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

                boolean isChecked;

                // TODO Auto-generated method stub

                binder.setsilent(arg1);         }

        });

          

         

          btn.setOnClickListener(new OnClickListener() {

               

                @Override

                public void onClick(View arg0) {

                    // TODO Auto-generated method stub

                   

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

                    player.start();

                }

            });

    }      

        @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);

        }

    }

 

 

BService

public class BService extends Service{

     

    MediaPlayer player;

    AudioManager audioManager;

    private int current;

    private boolean isquite=false;

    private MyBinder binder=new MyBinder();

 

   

    public class MyBinder extends Binder{

        public BService getService()

        {

            return BService.this;

           

        }

    }  

   

   

    public int getvolum(){

        current=audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

         return current;

    }

     public void setsilent(boolean isquite){

         if(isquite){

             getvolum();

             audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);

         }else{

             audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, current, 0);

         }

     }

         public void setvolum(int setting){

       

         audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, setting, 0);

             current=setting;

        

         }

   

        

   

   

   

   

    @Override

    public IBinder onBind(Intent arg0) {

        // TODO Auto-generated method stub

        System.out.println("Service is Binded");

        return binder;

    }

    public void onCreate() {

        // TODO Auto-generated method stub

        super.onCreate();

        System.out.println("Service is Created");

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

        player.start();

        audioManager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);

        super.onCreate();

        player.setOnCompletionListener(new OnCompletionListener() {

           

            @Override

            public void onCompletion(MediaPlayer arg0) {

                // TODO Auto-generated method stub

                player.start();

            }

        });

    }

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

        // TODO Auto-generated method stub

        player.start();

        return super.onStartCommand(intent, flags, startId);

    }

    public void onDestroy() {

        // TODO Auto-generated method stub

        super.onDestroy();

        if(player.isPlaying()){

             player.stop();

             System.out.println("Service is Destroyed");

    }

    }

 

 

 

运行截图:技术分享

 

 

 

 

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

实验的内容挺好玩的,根据老师讲的步骤跟方法都能成功的完成实验,十分有意义,下次再接再厉,可是还是有点遗憾,网络的播放方法无法播发。

实验评语

 

实验成绩

 

指导教师签名:              年   月   日

           

实验7 BindService 模拟通信

标签:

原文地址:http://www.cnblogs.com/lsq21/p/5454211.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!