(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 = intent.getStringExtra(EXTRA_PARAM2);
handleActionFoo(param1, param2);
} else if (ACTION_BAZ.equals(action)) {
final S

本文介绍了如何在Android Studio的备忘录应用中添加背景音乐。首先,需要将音乐文件放入project/app/src/main/res/raw目录下,并确保文件名使用英文。接着,在AndroidManifest.xml中进行相应配置。然后,创建MyIntentService类来处理音乐播放。在EditActivity中,整合音乐播放功能。最后,通过实际操作进行演示。
2766

被折叠的 条评论
为什么被折叠?



