一,简介
在上一篇文章中,初步了解了一些 Service 的工作属性,这篇文章将更深入地记录一下如何使用 Service .
二,Service 和 Thread
在学习使用之前,我们上一章完了应该还有一个疑问:我们确认了 service 如果需要做耗时操作,也必须要启动一个新的线程来实现,那么这样为何还需要启动一个新的 Service 呢?
1. 首先, 在 activity 中不好对 Thread 进行控制,back 键按一下就有可能丢失了线程的控制,而 Service 不会如此。
2. 线程的优先级太低,在 activity 结束之后, thread 会很容易被系统杀死。
3. Service 可以后台直接运行,不依赖于界面。IM 类软件可以与服务器保持心跳连接。
三,使用 Service
这里使用 Service 做一个简单的音乐播放的功能。我们需要有下面的这些功能:
- 查找手机里已经存在的音频
- 可以在某个界面控制音频的播放
- 可以在后台播放音频
根据这3点我们可以确定该 Service 是需要通过 startService 启动的。但控制方式可以有两种,第一是广播,第二是在 startService 之外另外还 bindService. 这里考虑到查找手机里已存在的音频可能是一个耗时的操作,把这个功能放到 Service 中实现,如此,我们需要讲大量数据从 Service 中传到 Activity 中,所以这里使用 bindService 实现。
首先,我们定义一个简单的界面:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:buttonBarButtonStyle">
<Button
android:id="@+id/music_pre"
android:text="@string/music_pre"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/music_ctrl"
android:text="@string/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/music_next"
android:text="@string/music_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<ListView
android:id="@+id/music_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
控制方面基本上需要的功能有上一曲,下一曲,播放,单击 ListView 时播放 选中的歌曲。
接下来,我们首先定义一个 Bean 文件,目前选择比较简单的方式,因此这里只定义了三个 Field,没有加上 setter, getter.
class Bean {
/**
* 显示的歌曲名称
*/
String name;
/**
* 歌曲路径
*/
String path;
/**
* 是否正在播放
*/
boolean playing = false;
@Override