广播
Android 应用与 Android 系统和其他 Android 应用之间可以相互收发广播消息,这与发布-订阅设计模式相似。这些广播会在所关注的事件发生时发送。举例来说,Android 系统会在发生各种系统事件时发送广播,例如系统启动或设备开始充电时。再比如,应用可以发送自定义广播来通知其他应用它们可能感兴趣的事件(例如,一些新数据已下载)。
广播消息本身会被封装在一个Intent对象中,该对象的操作字符串会标识所发生的事件(例如 android.intent.action.AIRPLANE_MODE )。该 Intent 可能还包含绑定到其 extra 字段中的附加信息。例如,飞行模式 intent 包含布尔值 extra 来指示是否已开启飞行模式。
服务
Service是一种可在后台执行长时间运行操作而不提供界面的应用组件。服务可由其他应用组件启动,而且即使用户切换到其他应用,服务仍将在后台继续运行。此外,组件可通过绑定到服务与之进行交互,甚至是执行进程间通信 (IPC)。例如,服务可在后台处理网络事务、播放音乐,执行文件 I/O 或与内容提供程序进行交互。
服务是一种即使用户未与应用交互也可在后台运行的组件,因此,只有在需要服务时才应创建服务。
创建服务,需创建 Service 的子类(或使用它的一个现有子类)。在实现中,需重写一些回调方法,从而处理服务生命周期的某些关键方面,并提供一种机制将组件绑定到服务(如适用)。以下为应重写的最重要的回调方法:
onStartCommand()
当另一个组件(如 Activity)请求启动服务时,系统会通过调用 startService() 来调用此方法。执行此方法时,服务即会启动并可在后台无限期运行。如果您实现此方法,则在服务工作完成后,需负责通过调用 stopSelf() 或 stopService() 来停止服务。(如果只想提供绑定,则无需实现此方法。)
onBind()
当另一个组件想要与服务绑定(例如执行 RPC)时,系统会通过调用 bindService() 来调用此方法。在此方法的实现中,必须通过返回 IBinder提供一个接口,以供客户端用来与服务进行通信。请务必实现此方法;但是,如果您并不希望允许绑定,则应返回 null。
onCreate()
首次创建服务时,系统会(在调用 onStartCommand()或 onBind()之前)调用此方法来执行一次性设置程序。如果服务已在运行,则不会调用此方法。
onDestroy()
当不再使用服务且准备将其销毁时,系统会调用此方法。服务应通过实现此方法来清理任何资源,如线程、注册的侦听器、接收器等。这是服务接收的最后一个调用。
代码实现
(一)AndroidManifest.xml 注册服务
<service android:name=".MusicService"></service>
(二)编写xml布局
<ImageButton android:id="@+id/pext"
...
android:src="@drawable/pext_"/>
<ImageButton android:id="@+id/play"
...
android:src="@drawable/play"/>
<ImageButton android:id="@+id/pause"
...
android:src="@drawable/media_"/>
<ImageButton android:id="@+id/next"
...
android:src="@drawable/next_"/>
<LinearLayout android:orientation="vertical"
...
<TextView android:id="@+id/title"
...
android:marqueeRepeatLimit="marquee_forever"/>
<TextView android:id="@
...
android:layout_height="wrap_content"/>
实现效果
(三)创建assets文件夹,上传mp3资源
(四)实现服务
MusicService.java:
MediaPlayer mPlayer;
// 当前的状态,0x11代表没有播放;0x12代表正在播放;0x13代表暂停
int status = 0x11;
// 记录当前正在播放的音乐 int current = 0;
// 创建BroadcastReceiver
serviceReceiver = new MyReceiver();
// 创建MediaPlayer
mPlayer = new MediaPlayer();
// 为MediaPlayer播放完成事件绑定监听器
mPlayer.setOnCompletionListener(new OnCompletionListener(){...}
public class MyReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, Intent intent) {
int control = intent.getIntExtra("control", -1);
switch (control) {
// 播放或暂停
case 1:{...}
// 停止播放
case 2:{...}
// 播放到第一首
case 3:{...}
// 播放到最后一首
case 4:{...}
fineFragment.Java:
// 获取程序界面界面中的两个按钮
play = (ImageButton) view.findViewById(R.id.play);
stop = (ImageButton) view.findViewById(R.id.pause);
title = (TextView) view.findViewById(R.id.title);
author = (TextView) view.findViewById(R.id.author);
//获取上一首、下一首按钮
button1 = (ImageButton) view.findViewById(R.id.pext);
button2 = (ImageButton) view.findViewById(R.id.next);
// 为两个按钮的单击事件添加监听器
play.setOnClickListener(this); stop.setOnClickListener(this);
//为上一首、下一首按钮添加监听器
button1.setOnClickListener(this); button2.setOnClickListener(this);
// 指定BroadcastReceiver监听的Action
filter.addAction(UPDATE_ACTION);
// 注册BroadcastReceiver
getActivity().registerReceiver(activityReceiver, filter);
Intent intent = new Intent(getActivity(), MusicService.class);
// 启动后台Service
getActivity().startService(intent);
// 发送广播,将被Service组件中的BroadcastReceiver接收到
getActivity().sendBroadcast(intent);
(五)效果图
播放:
暂停:
上一首
下一首