MainActivity.java
package com.example.rrt;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
// 创建标签
private static final String TAG = "PlayMusic";
// 获取按钮
private Button playBtn;
private Button stopBtn;
private Button pauseBtn;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findView();
bindButton();
}
private void findView()
{
playBtn = (Button)findViewById(R.id.play);
stopBtn = (Button)findViewById(R.id.stop);
pauseBtn = (Button)findViewById(R.id.pause);
}
// 为按钮绑定事件
private void bindButton()
{
playBtn.setOnClickListener(this);
stopBtn.setOnClickListener(this);
pauseBtn.setOnClickListener(this);
}
// 重写onClick事件
// 通过判断按钮ID,做不同处理
@Override
public void onClick(View v) {
Log.d(TAG, "onClick : onClick()");
int op = -1;
// Intent: 组件之间的通讯中,主要是由Intent协助完成的。
// 参考: http://bbs.hiapk.com/thread-7959-1-1.html
Intent intent = new Intent("com.yang.Android.MUSIC");
switch (v.getId()) {
case R.id.play: // 播放
Log.d(TAG, "onClick : play music");
op = 1;
break;
case R.id.stop: // 停止
Log.d(TAG, "onClick : stop music");
op = 2;
break;
case R.id.pause: // 暂停
Log.d(TAG, "onClick : pause music");
op = 3;
break;
}
// Bundle 是Android开发中的一个类,用于Activity之间传输数据用。
// 参考: http://zhidao.baidu.com/question/187391685.html
Bundle bundle = new Bundle();
bundle.putInt("op", op);
intent.putExtras(bundle);
// Service 是android 系统中的一种组件,它跟Activity的级别差不多,但是他不能自己运行,只能后台运行
// 参考: http://ant-qingyun27sc.iteye.com/blog/1596073
startService(intent);
}
}
MusicService.java
package com.example.rrt;
import java.io.IOException;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
public class MusicService extends Service {
private static final String TAG = "MyService";
private MediaPlayer mediaPlayer;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
Log.v(TAG, "onCreate");
if(mediaPlayer == null)
{
mediaPlayer = MediaPlayer.create(this, R.raw.test);
mediaPlayer.setLooping(false);
}
}
@Override
public void onDestroy() {
Log.v(TAG, "onDestroy");
if(mediaPlayer != null)
{
mediaPlayer.stop();
mediaPlayer.release();
}
}
@Override
public void onStart(Intent intent, int startId)
{
Log.v(TAG,"onStart");
if(intent != null)
{
Bundle bundle = intent.getExtras();
if(bundle != null)
{
int op = bundle.getInt("op");
switch(op) {
case 1:
play();
break;
case 2:
stop();
break;
case 3:
pause();
break;
}
}
}
}
public void play() {
Log.v(TAG,"play");
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
}
public void pause() {
Log.v(TAG,"pause");
if(mediaPlayer != null && mediaPlayer.isPlaying())
{
mediaPlayer.pause();
}
}
public void stop() {
Log.v(TAG,"stop");
if( mediaPlayer != null )
{
mediaPlayer.stop();
try{
mediaPlayer.prepare();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
AndroidMainfest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rrt"
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.rrt.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>
<? 这里需要注册 "MusicService" ?>
<service android:name=".MusicService" >
<intent-filter>
<action android:name="com.yang.Android.MUSIC" />
<category android:name="android.intent.category.default" />
</intent-filter>
</service>
</application>
</manifest>