MainActivity.Java
package com.example.week5_day1_servicebind;
import com.example.week5_day1_servicebind.MyMusicService.MyBinder;
import android.os.Bundle;
import android.os.IBinder;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.View;
public class MainActivity extends Activity {
private ServiceConnection conn;
private MyMusicService musicService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
conn = new Myconn();//建立服务连接对象
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:// 启动音乐
if (musicService == null) {
Intent intent = new Intent(MainActivity.this,
MyMusicService.class);
bindService(intent, conn, Context.BIND_AUTO_CREATE);//绑定服务,后台也能播放
} else {
musicService.playMusic();
}
break;
case R.id.btn2:// 暂停音乐
if (musicService != null) {
musicService.pasueMusic();
}
break;
case R.id.btn3:// 关闭音乐
if (musicService != null) {
musicService.stopMusic();
musicService = null;
unbindService(conn);
}
break;
default:
break;
}
}
/**
* 服务连接
*/
public class Myconn implements ServiceConnection {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
MyBinder myBinder = (MyBinder) service;
musicService = myBinder.getMusicService();
}
@Override
public void onServiceDisconnected(ComponentName name) {
conn = null;
}
}
}
播放音乐服务
package com.example.week5_day1_servicebind;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Binder;
import android.os.IBinder;
public class MyMusicService extends Service {
private MediaPlayer mediaPlayer;//声明媒体播放器
private int pos = 0;//声明播放进度
/**
* 通过 binderService()绑定服务 启动后回调的方法
*/
@Override
public IBinder onBind(Intent intent) {
playMusic();
return new MyBinder();
}
public class MyBinder extends Binder {
public MyMusicService getMusicService() {
return MyMusicService.this;
}
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
if (mediaPlayer == null) {
mediaPlayer = mediaPlayer.create(MyMusicService.this,
R.raw.heavencity);
mediaPlayer.setLooping(false);// 设置不需要单曲循环
}
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mediaPlayer.release();// 释放资源
}
});
}
// 播放音乐
public void playMusic() {
if (mediaPlayer != null && !mediaPlayer.isLooping()) {
try {
if (pos != 0) {
mediaPlayer.seekTo(pos);// 根据指定位置进行播放
mediaPlayer.start();
} else {
mediaPlayer.stop();
mediaPlayer.prepare();// 重新启动之前必须prepare
mediaPlayer.start();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 暂停音乐
public void pasueMusic() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
pos = mediaPlayer.getCurrentPosition();
mediaPlayer.pause();
}
}
// 停止音乐
public void stopMusic() {
if (mediaPlayer != null) {
mediaPlayer.stop();//停止
mediaPlayer.release();// 资源重置
}
}
}
布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="播放音乐" />
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="暂停音乐" />
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="关闭音乐" />
</LinearLayout>
清单配置文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.week5_day1_servicebind"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.week5_day1_servicebind.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 注册服务 -->
<service android:name=".MyMusicService"></service>
</application>
</manifest>