一、整体布局
二、 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true" android:name=".AudioService"/>
</application>
</manifest>
二、Java
1.MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btn1,btn2;
Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.button1);
btn2=(Button)findViewById(R.id.button2);
btn1.setOnClickListener(new myClick());
btn2.setOnClickListener(new myClick());
intent = new Intent(MainActivity.this, AudioService.class);
}
class myClick implements View.OnClickListener {
public void onClick(View v){
if (v==btn1)
{
MainActivity.this.startService(intent);
}
if (v==btn2)
{
MainActivity.this.stopService(intent);
}
}
}
}
2.AudioService.java
package com.example.myapplication;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.widget.Toast;
public class AudioService extends Service {
MediaPlayer play;
public IBinder onBind(Intent intent){return null;}
public void onCreate()
{
super.onCreate();
play=MediaPlayer.create(this,R.raw.wojide);
toastMessage("创建后台服务");
}
public int onStartCommand(Intent intent,int flags,int startId)
{
super.onStartCommand(intent,flags,startId);
play.start();
toastMessage("开始播放");
return START_STICKY;
}
public void onDestroy()
{
play.release();
super.onDestroy();
toastMessage("停止播放");
}
private void toastMessage(String message) {
Toast toast=Toast.makeText(AudioService.this,message,Toast.LENGTH_LONG);
toast.show();
}
}
三、xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="160dp"
android:layout_height="60dp"
android:layout_marginStart="140dp"
android:layout_marginLeft="140dp"
android:layout_marginTop="436dp"
android:text="播放音乐服务器"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="160dp"
android:layout_height="60dp"
android:text="关闭音乐服务器"
app:layout_constraintStart_toStartOf="@+id/button1"
app:layout_constraintTop_toBottomOf="@+id/button1" />
</androidx.constraintlayout.widget.ConstraintLayout>
四、在res下新建一个名为raw,把MP3格式音乐放里面