(1)在project/app/src/main/res/raw目录下添加音乐(命名要是英文的)
(2)在AndroidMainfest.xml中的代码
<service
android:name=".MyIntentService"
android:exported="false" />
<activity android:name=".EditActivity" />
<activity android:name=".Alarm.EditAlarmActivity" />
<activity android:name=".Alarm.AlarmActivity" />
<receiver android:name=".Alarm.AlarmReceiver" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
(3)建立类MyIntentService
package com.example.notebook;
import android.app.IntentService;
import android.content.Intent;
import android.media.MediaPlayer;
public class MyIntentService extends IntentService {
// TODO: Rename actions, choose action names that describe tasks that this
// IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS
public static final String ACTION_FOO = "com.example.notebook.action.FOO";
public static final String ACTION_BAZ = "com.example.notebook.action.BAZ";
// action声明 这里有五个背景音乐
public static final String ACTION_MUSIC_1 = "com.example.notebook.action.music1";
public static final String ACTION_MUSIC_2 = "com.example.notebook.action.music2";
public static final String ACTION_MUSIC_3 = "com.example.notebook.action.music3";
public static final String ACTION_MUSIC_4 = "com.example.notebook.action.music4";
public static final String ACTION_MUSIC_5 = "com.example.notebook.action.music5";
public static final String ACTION_MUSIC_6 = "com.example.notebook.action.music6";
// TODO: Rename parameters
public static final String EXTRA_PARAM1 = "com.example.notebook.extra.PARAM1";
public static final String EXTRA_PARAM2 = "com.example.notebook.extra.PARAM2";
// 声明MediaPlayer对象
private static MediaPlayer mediaPlayer;
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
final String action = intent.getAction();
if (intent != null) {
if (ACTION_FOO.equals(action)) {
final String param1 = intent.getStringExtra(EXTRA_PARAM1);
final String param2