(1)在project/app/src/main/res/raw目录下添加音乐(命名要是英文的)
(2)在AndroidMainfest.xml中的代码
(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 String param1 = intent.getStringExtra(EXTRA_PARAM1);
final String param2 = intent.getStringExtra(EXTRA_PARAM2);
handleActionBaz(param1, param2);
}
// 根据intent设置的action来执行对应服务的操作
if (ACTION_MUSIC_1.equals(action)){
handleActionMusic1();
}else if (ACTION_MUSIC_2.equals(action)){
handleActionMusic2();
}else if (ACTION_MUSIC_3.equals(action)){
handleActionMusic3();
}else if (ACTION_MUSIC_4.equals(action)){
handleActionMusic4();
}else if (ACTION_MUSIC_5.equals(action)){
handleActionMusic5();
}else if (ACTION_MUSIC_6.equals(action)){
handleActionMusic6();
}
}
}
private void handleActionMusic1() {
if(mediaPlayer != null) mediaPlayer.stop();
// 根据音乐资源文件创建MediaPlayer对象 设置循环播放属性 开始播放
mediaPlayer = MediaPlayer.create(this, R.raw.run);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
private void handleActionMusic2() {
if(mediaPlayer != null) mediaPlayer.stop();
// 根据音乐资源文件创建MediaPlayer对象 设置循环播放属性 开始播放
mediaPlayer = MediaPlayer.create(this, R.raw.dream_wedding);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
private void handleActionMusic3() {
if(mediaPlayer != null) mediaPlayer.stop();
// 根据音乐资源文件创建MediaPlayer对象 设置循环播放属性 开始播放
mediaPlayer = MediaPlayer.create(this, R.raw.be_quiet);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
private void handleActionMusic4() {
if(mediaPlayer != null) mediaPlayer.stop();
// 根据音乐资源文件创建MediaPlayer对象 设置循环播放属性 开始播放
mediaPlayer = MediaPlayer.create(this, R.raw.girly_heart);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
private void handleActionMusic5() {
if(mediaPlayer != null) mediaPlayer.stop();
// 根据音乐资源文件创建MediaPlayer对象 设置循环播放属性 开始播放
mediaPlayer = MediaPlayer.create(this, R.raw.sword_fairy);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
private void handleActionMusic6() {
if(mediaPlayer != null) mediaPlayer.stop();
// 根据音乐资源文件创建MediaPlayer对象 设置循环播放属性 开始播放
mediaPlayer = MediaPlayer.create(this, R.raw.perfect_encounter);
mediaPlayer.setLooping(true);
mediaPlayer.start();
}
private void handleActionFoo(String param1, String param2) {
// TODO: Handle action Foo
throw new UnsupportedOperationException("Not yet implemented");
}
private void handleActionBaz(String param1, String param2) {
// TODO: Handle action Baz
throw new UnsupportedOperationException("Not yet implemented");
}
}
(4)EditActivity
public boolean onOptionsItemSelected(MenuItem item) {
final String[] colors={"护眼色", "紫罗兰", "道奇蓝","碧绿色","热情粉","纯白色"};
final String[] musics={"奔跑吧","梦中的婚礼","安静","少女的心","剑仙","完美的邂逅"};
switch (item.getItemId()){
case R.id.delete:
new alertDialog.Builder(EditActivity.this)
.setMessage("确定删除当前便签吗?")
.setPositiveButton("确定", new DialogInterface.onClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(openMode==4){
intent.putExtra("mode",-1); //新笔记,返回-1,什么也不做
}else {
intent.putExtra("mode",2); //已经存在的笔记,用于返回操作
intent.putExtra("id",id);
}
setResult(RESULT_OK,intent);
finish();
}
}).setNegativeButton("取消", new DialogInterface.onClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create().show();
break;
case R.id.read:
Drawable drawable = item.getIcon(); //图标颜色变换
if(isRead){
drawable.setColorFilter(null); //消除上一级的改变
drawable.setColorFilter(getResources().getColor(R.color.greyC), PorterDuff.Mode.SRC_IN);
toast1.setText("您已进入编辑模式");
toast2.show();
editText.setFocusableInTouchMode(true);
editText.setFocusable(true);
isRead = false;
}else{
drawable.setColorFilter(null);
drawable.setColorFilter(getResources().getColor(R.color.p), PorterDuff.Mode.SRC_IN);
toast1.setText("您已进入阅读模式");
toast1.show();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
editText.setFocusableInTouchMode(false);
editText.setFocusable(false);
isRead = true;
}
break;
case R.id.change:
new alertDialog.Builder(EditActivity.this)
.setTitle("选择一个背景色")
.setIcon(R.drawable.tomato)
.setItems(colors, new DialogInterface.onClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
MainActivity.curId = 0;
break;
case 1:
MainActivity.curId = 1;
break;
case 2:
MainActivity.curId = 2;
break;
case 3:
MainActivity.curId = 3;
break;
case 4:
MainActivity.curId = 4;
break;
case 5:
MainActivity.curId = 5;
break;
default:
break;
}
getWindow().setBackgroundDrawableResource(curColor[MainActivity.curId]);
}
}).create().show();
break;
case R.id.music:
new alertDialog.Builder(EditActivity.this)
.setTitle("选择一个背景音乐")
.setIcon(R.drawable.music_collection)
.setItems(musics, new DialogInterface.onClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case 0:
// 启动服务播放背景音乐
intentMusic = new Intent(EditActivity.this, MyIntentService.class);
String action_1 = MyIntentService.ACTION_MUSIC_1;
// 设置action
intentMusic.setAction(action_1);
startService(intentMusic);
break;
case 1:
// 启动服务播放背景音乐
intentMusic = new Intent(EditActivity.this, MyIntentService.class);
String action_2 = MyIntentService.ACTION_MUSIC_2;
// 设置action
intentMusic.setAction(action_2);
startService(intentMusic);
break;
case 2:
// 启动服务播放背景音乐
intentMusic = new Intent(EditActivity.this, MyIntentService.class);
String action_3 = MyIntentService.ACTION_MUSIC_3;
// 设置action
intentMusic.setAction(action_3);
startService(intentMusic);
break;
case 3:
// 启动服务播放背景音乐
intentMusic = new Intent(EditActivity.this, MyIntentService.class);
String action_4 = MyIntentService.ACTION_MUSIC_4;
// 设置action
intentMusic.setAction(action_4);
startService(intentMusic);
break;
case 4:
// 启动服务播放背景音乐
intentMusic = new Intent(EditActivity.this, MyIntentService.class);
String action_5 = MyIntentService.ACTION_MUSIC_5;
// 设置action
intentMusic.setAction(action_5);
startService(intentMusic);
break;
case 5:
// 启动服务播放背景音乐
intentMusic = new Intent(EditActivity.this, MyIntentService.class);
String action_6 = MyIntentService.ACTION_MUSIC_6;
// 设置action
intentMusic.setAction(action_6);
startService(intentMusic);
break;
}
}
}).create().show();
break;
}
return super.onOptionsItemSelected(item);
}
(5)演示
作者:董媛媛



