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

实验7

时间:2016-05-02 22:37:21      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

实验报告

课程名称

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

实验日期

2016年4月22日

实验项目名称

 BindService模拟通信

实验地点

S30010

实验类型

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

学  时

2

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

1.目的:实现启动端和BindService之间的双向通信

2.要求:实现从启动端传递一个数据至BindService端;

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

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

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

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

(1)PC机

(2)操作系统:Windows XP

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

三、实验内容及步骤

1)完善Activity类

2)新建Service类

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

 

代码:

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;

public class BindService extends Service{
    
private MediaPlayer mp;
    
private int count;
private boolean quit;
private MyBinder binder=new MyBinder();
public class MyBinder extends Binder{
    public int getCount()
    {
        return count;
        
    }
    public void MusicStar()
    {
        mp=MediaPlayer.create(BindService.this,R.raw.big);
        mp.start();
    }
    public void MusicPause()
    {
        mp.pause();
    }
    public void MusicStop()
    {
        mp.stop();
    }
}
    @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();
            
        }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        //mp.start();
        //mp.pause();
        return super.onStartCommand(intent, flags, startId);
    }
    @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");
        }

}
package com.example.bindservicedemo;

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.media.AudioManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
private Button bind,unbind,getServiceStatus,intentServiceButton,comServiceButton,star,stop,pause,voicejia,voicejian;
private BindService.MyBinder binder;
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=(BindService.MyBinder) arg1;
    }
};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bind=(Button)findViewById(R.id.button1);
        unbind=(Button)findViewById(R.id.button3);
        getServiceStatus=(Button)findViewById(R.id.button2);
        intentServiceButton=(Button)findViewById(R.id.button4);
        comServiceButton=(Button)findViewById(R.id.button5);
        star=(Button) findViewById(R.id.buttonStar);
        pause=(Button) findViewById(R.id.buttonPause);
        stop=(Button) findViewById(R.id.buttonStop);
        voicejian=(Button) findViewById(R.id.buttonVoicejian);
        voicejia=(Button) findViewById(R.id.buttonVoicejia);
        final Intent intent=new Intent(MainActivity.this,BindService.class);
        //绑定方式创建Service
        bind.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                bindService(intent, conn, Service.BIND_AUTO_CREATE);
            }
        });
        //解绑Service
        unbind.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                unbindService(conn);
            }
        });
        //获取Service状态
        getServiceStatus.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, "Service 的count值为:"+binder.getCount(), Toast.LENGTH_LONG).show();
            }
        });
        //普通Service创建方式
        comServiceButton.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent=new Intent(MainActivity.this,ComService.class);
                startService(intent);
            }
        });
        //使用IntentService方式创建
        intentServiceButton.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent=new Intent(MainActivity.this,MyIntentService.class);
                startService(intent);
            }
        });
        
        star.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                binder.MusicStar();
            }
        });
        
        pause.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                binder.MusicPause();
            }
        });
        
        stop.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                binder.MusicStop();
            }
        });
        
        voicejian.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                AudioManager mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
                mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_LOWER,
                        AudioManager.FX_FOCUS_NAVIGATION_UP);

            }
        });
        
        voicejia.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                AudioManager mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
                mAudioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC,AudioManager.ADJUST_RAISE,
                        AudioManager.FX_FOCUS_NAVIGATION_UP);

            }
        });
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

 

运行结果:(截图)

技术分享

 

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

这次实验相对来说比较难,也睡通过别人的帮助和上网找资料才勉强完成,感觉掌握得还是不够。

实验评语

 

实验成绩

 

指导教师签名:              年   月   日

           

实验7

标签:

原文地址:http://www.cnblogs.com/woaiQ1314/p/5453136.html

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