界面展示需要两个按钮,一个按钮为开始播放,一个为停止播放:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/startplay"/>
<Button
android:id="@+id/stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/stopplay"
/>
</LinearLayout>
String.xml:
<resources>
<string name="app_name">PlayMusicService</string>
<string name="hello">PlayMusicService Activity</string>
<string name="startplay">开始播放</string>
<string name="stopplay">停止播放</string>
</resources>
AndroidManifest.xml中需要定一个Activity以及一个播放音乐的Service。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.playmusicservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity android:name="PlayMusicActivity"
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="MusicService">
<intent-filter>
<action android:name="com.playmusicservice.MUSIC" />
<category android:name="android.intent.category.default" />
</intent-filter>
</service>
</application>
</manifest>
res目录下增加需要播放的音乐资源:
这是后我们看R.java中已经将资源的目录配置好了:
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package com.playmusicservice;
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int start=0x7f070000;
public static final int stop=0x7f070001;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class raw {
public static final int test=0x7f040000;
}
public static final class string {
public static final int app_name=0x7f050000;
public static final int hello=0x7f050001;
public static final int startplay=0x7f050002;
public static final int stopplay=0x7f050003;
}
public static final class style {
/**
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
Base application theme for API 11+. This theme completely replaces
AppBaseTheme from res/values/styles.xml on API 11+ devices.
API 11 theme customizations can go here.
Base application theme for API 14+. This theme completely replaces
AppBaseTheme from BOTH res/values/styles.xml and
res/values-v11/styles.xml on API 14+ devices.
API 14 theme customizations can go here.
*/
public static final int AppBaseTheme=0x7f060000;
/** Application theme.
All customizations that are NOT specific to a particular API-level can go here.
*/
public static final int AppTheme=0x7f060001;
}
}
这时候我们增加播放音乐的服务类:
package com.playmusicservice;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
public class MusicService extends Service{
//MediaPlayer对象
private MediaPlayer player;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
public void onStart(Intent intent,int startId){
super.onStart(intent, startId);
//这里可以直接为装在音乐文件
player = MediaPlayer.create(this, R.raw.test);
//开始播放
player.start();
}
public void onDestroy(){
super.onDestroy();
//停止音乐-----停止service
player.stop();
}
}
主界面可以这样写:
package com.playmusicservice;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class PlayMusicActivity extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//从main.xml布局中获得Button对象
Button button_start = (Button)findViewById(R.id.start);
Button button_stop = (Button)findViewById(R.id.stop);
button_start.setOnClickListener(start);
button_stop.setOnClickListener(stop);
}
private OnClickListener start = new OnClickListener(){
@Override
public void onClick(View arg0) {
startService(new Intent("com.playmusicservice.MUSIC"));
}
};
private OnClickListener stop = new OnClickListener(){
@Override
public void onClick(View arg0) {
stopService(new Intent("com.playmusicservice.MUSIC"));
}
};
}