Activity与Service之间交互并播放歌曲的实现代码
Activity与Service之间交互并播放歌曲,为了方便,我把要播放的歌曲定死了,大家可以灵活改进

MService:
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
public class MService extends Service{
MyBinder myBinder = new MyBinder();
private MediaPlayer mediaPlayer;
public class MyBinder extends Binder{
MService getService(){
return MService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
Log.v("CAT", "onBind");
return myBinder;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.v("CAT", "onCreate");
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(Environment.getExternalStorageDirectory() + "/mp3/trhxn.mp3");
mediaPlayer.prepare();
} catch (Exception e) {
// TODO Auto-generated catch block
Log.v("CAT", "fail");
e.printStackTrace();
}
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.v("CAT", "onDestroy");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Log.v("CAT", "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Log.v("CAT", "onUnbind");
return false;
}
public void start(){
mediaPlayer.start();
}
public void pause(){
mediaPlayer.pause();
}
public void stop(){
mediaPlayer.stop();
mediaPlayer.release();
}
public int getDuration(){
return mediaPlayer.getDuration();
}
public int getCurrentPosition(){
return mediaPlayer.getCurrentPosition();
}
public void seekTo(int position){
mediaPlayer.seekTo(position);
}
}
MusicPlayActivity:
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
public class MusicPlayActivity extends Activity {
/** Called when the activity is first created. */
MService mService;
private ServiceConnection conn = new ServiceConnection(){
@Override
public void onServiceConnected(ComponentName arg0, IBinder arg1) {
// TODO Auto-generated method stub
mService = ((MService.MyBinder)arg1).getService();
Log.v("CAT", "getServiced");
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
mService = null;
}
};
private SeekBar seekBar;
private Button playBT;
private boolean isPlaying = false;
private boolean isBinded = false;
private Handler mHandler;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(MusicPlayActivity.this, MService.class);
if(!isBinded){
bindService(intent, conn, BIND_AUTO_CREATE);
isBinded = true;
}
seekBar = (SeekBar) findViewById(R.id.seekBar);
playBT = (Button) findViewById(R.id.startBT);
mHandler = new Handler();
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if(fromUser){
mService.seekTo((progress*mService.getDuration())/100);
}
}
});
playBT.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!isPlaying){
mService.start();
isPlaying = true;
playBT.setText("暂停");
mHandler.post(seekBarThread);
}else{
mService.pause();
isPlaying = false;
playBT.setText("播放");
mHandler.removeCallbacks(seekBarThread);
}
}
});
}
Runnable seekBarThread = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
seekBar.setProgress((mService.getCurrentPosition()*100)/mService.getDuration());
mHandler.postDelayed(seekBarThread, 200);
}
};
}
Activity与Service之间交互并播放歌曲的实现代码的更多相关文章
- activity与service之间的通信方式
Activity之间的通信 1.activity与activity的通信可以通过Intent来封装数据,startActivityForResult()来实现,当跳转的activity调用finish ...
- activity 与 service 之间的通信
activity和service通信:通过binder 举个我实际项目中的例子:在service中下载更新应用 首先是下载更新apk的service: public class UpdateVersi ...
- Activity 与 Service 之间的消息传递
BgService代码 public class BgService extends Service { public static final String TAG = "BgServic ...
- 通过bind实现activity与service的交互
先点bind按钮实现onCreate,在点击start给service传值(get方法) xml: <RelativeLayout xmlns:android="http://sche ...
- Activity与Service数据交互:Binder、bindService的用法
package com.lixu.jiaohu; import com.lixu.jiaohu.MyAppService.Mybind; import android.app.Activity; im ...
- Android四大组件应用系列——Activity与Service交互实现APK下载
Servic与Activity相比它没有界面,主要是在后台执行一些任务,Service有两种启动方法startService()和bindService(),startService方式Service ...
- Android中Activity、Service和线程之间的通信
Activity.Service和线程应该是Android编程中最常见的几种类了,几乎大多数应用程序都会涉及到这几个类的编程,自然而然的,也就会涉及到三者之间的相互通信,本文就试图简单地介绍一下这三者 ...
- 201709012工作日记--activity与service的通信机制
service生命周期 Service主要包含本地类和远程类. Service不是Thread,Service 是android的一种机制,当它运行的时候如果是Local Service,那么对应的 ...
- Android Activity与Service的交互方式
参考: http://blog.csdn.net/gebitan505/article/details/18151203 实现更新下载进度的功能 1. 通过广播交互 Server端将目前的下载进度,通 ...
随机推荐
- python 自动认证登录
import urllib import base64 import urllib2 def auto_login(urllink,username,password): authstr = 'Bas ...
- iOStextView的代理方法展示
UITextView的代理方法 textViewShouldBeginEditing: and textViewDidBeginEditing: - (BOOL)textViewShouldBegin ...
- 【2】docker 与UFW
ufw是一个主机端的iptables类防火墙配置工具. 1.查看ufw的状态:ufw status 2.如果使用UFW,需要对其进行修改才能让DOCKER工作.docker使用一个网桥来管理容器中的网 ...
- html 细线表格
可以<table width="800px" cellpadding="0" border="1px" style="bor ...
- hdu_3294_Girls' research(Manacher)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3294 题意:给你一个字符和一个字符串,第一个字符表示该字符代表a,然后让你求变换后的最长回文区间并输出 ...
- hdu_4521_小明系列问题——小明序列(LIS)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4521 题意:中文题,不解释 题解:这题就是LIS的加强版,可以用二分的nlogn来做,也可以用线段树的 ...
- 让AutoMapper在你的项目里飞一会儿(转)
出处:http://www.cnblogs.com/WeiGe/p/3835523.html 先说说DTO DTO是个什么东东? DTO(Data Transfer Object)就是数据传输对象,说 ...
- bg-route
1.目录 homework add.html online.html offline.html res script action homework add.js 2.add.html<div ...
- GsonFormat 报错
GsonFormat原来也有bug 我是用GsonFormat来生成java bean的,但是运行起来居然报 Caused by: java.lang.NumberFormatException: E ...
- jsp 页面取值
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...