amdroid studio给备忘录添加背景音乐

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

(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 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值