一、Service简述
在Android中,Service是一个用来执行长时间运行且没有用户界面的操作的组件。它可以在后台执行任务,即使用户切换到其他应用程序也不会被中断。Service通常用于执行不需要与用户交互的操作,如下载文件、播放音乐等。
二、点击事件-播放音乐服务demo
1.MusicPlayService类的创建
创建空白项目ServiceTest后,在app/src/main/java/com.example.servicetest路径下新建Java class并命名为MusicPlayService。与MainActivity类的代码结构相似,MusicPlayService继承自Service类,重写onCreate()、onStartCommand、onDestroy()和onBind(Intent intent)方法。
package com.example.servicetest;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.widget.Toast;
public class MusicPlayService extends Service {
private MediaPlayer mediaPlayer; // 私有变量
@Override
public void onCreate() {
super.onCreate();
// 创建MediaPlayer对象,并设置音乐资源
mediaPlayer = MediaPlayer.create(this, R.raw.example_music); // 假设sample_music.mp3文件在res/raw/目录下
mediaPlayer.setLooping(true); // 设置循环播放
Toast.makeText(this, "Service Created", Toast.LENGTH_SHORT).show();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) { // Service的启动
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start(); // 开始播放
Toast.makeText(this, "Music Playing", Toast.LENGTH_SHORT).show();
}
return START_STICKY; // 如果希望Service在被杀死后自动重启,则返回START_STICKY
}
@Override
public void onDestroy() {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop(); // 停止播放
}
mediaPlayer.release(); // 释放资源空间
Toast.makeText(this, "Music Stopped", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
onCreate()方法创建服务-音乐播放对象,通过onStartCommand()执行播放,通过onDestory()结束播放。由于该音乐播放为启动服务,不需要与客户端进行长时间的交互操作,一旦启动,服务可以在后台无限期运行,即使启动它的组件被销毁也是如此。对于这种服务,onBind方法返回null,因为服务不需要与客户端绑定。
mediaPlayer = MediaPlayer.create(this, R.raw.example_music); // 假设sample_music.mp3文件在res/raw/目录下
mediaPlayer.setLooping(true); // 设置循环播放
上述一段代码onStartCommand()中创建mediaPlayer实例,this参数表示有效的Context上下文对象。由于MusicPlayService是一个Service的子类,可以直接使用this作为Context参数。
R.raw.example_music参数,R 是一个自动生成的类,用于提供对资源(布局文件、字符串、图片、音频文件等)的引用,在项目的 res/目录下添加资源时,Android构建系统会自动生成相应的引用ID到 R 类中。example_music是自定义的文件名。这里在res/路径下直接新建了raw的文件夹,方便使用MediaPlayer播放Mp3音乐。
Toast.makeText(this, "Service Created", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Music Playing", Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Music Stopped", Toast.LENGTH_SHORT).show();
Toast用于消息弹窗显示,其Toast.makeText方法同样需要有效的Context参数(this),另外Toast.LENGTH_SHORT)表示预定义的可选参数值(较短的显示时间,大约2秒),也能选择Toast.LENGTH_LONG(较长的显示时间,大约3.5秒)。
2.在AndroidManifest.xml中声明Service
在<application···>···</application>中的位置,按照<activity···>···</activity>相同格式声明service。确保项目中有一个名为example_music.mp3的音乐文件(自定义名)放置在res/raw/目录下。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.servicetest">
<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/Theme.ServiceTest">
<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:name=".MusicPlayService"/>
</application>
</manifest>
3.创建Activity来控制音乐播放
在MainActivity类中设计事件来开启\结束音乐播放。
package com.example.servicetest;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
// 被调用以启动Service并播放音乐
public void startMusicService(View view) {
Intent serviceIntent = new Intent(this, MusicPlayService.class);
startService(serviceIntent);
}
public void stopMusicService(View view) {
Intent serviceIntent = new Intent(this, MusicPlayService.class);
stopService(serviceIntent);
}
}
这里定义了两个方法startMusicService(View view)和stopMusicService(View view),view参数代表被点击的视图对象。方法中利用Intent来传递MainActivity和MusicPlayService两个类(活动与服务)之间信息,this参数同样表示Context上下文。
4.在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/startMusicServiceButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="startMusicService"
android:text="Play Music"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.269"
tools:ignore="MissingConstraints" />
<Button
android:id="@+id/stopMusicServiceButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="stopMusicService"
android:text="Stop Music"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.443"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
Button的android:onClick属性,允许开发者直接在XML中指定方法名,当用户点击该视图(此处为按钮)时,直接通过属性值调用与之关联的Activity中的同名方法,即第3小节中的public公共方法:startMusicService(View view)和stopMusicService(View view)。这种方式可以避免在Activity的onCreate方法中通过调用findViewById和setOnClickListener来设置点击事件,从而简化代码。但如果Activity中没有找到对应的同名方法,应用将在运行时抛出NoSuchMethodException异常。
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.443"
这几行定义Button的位置,能实现按钮放置在任意位置。
三、执行结果展示
采用虚拟机展示测试结果,能正常播放、停止音乐即成功。
四、思考
该demo利用Service与Activity的Button,实现一个简单的音乐播放服务,其中展示了一般Service创建、启动和销毁的生命周期,但没有绑定具体的客户端。
主要收获如下:
(1)Service的一般创建和调用方法;
(2)Service和Activity之间的交互;
(3)Intent、Toast、onBind、android:onClick等方法或属性的使用示例;
反馈问题如下:
(1)Service绑定客户端的实例demo应该如何制作;
(2)Handler机制和Binder、Messenger、BroadcastReceiver等方法在Service中的使用应该如何实现;
(3)Service默认在主线程执行,如何手动开启新线程来执行。