Service and Broadcast
这里我们首先要说的是android的广播机制,它的形式是这样的:
应用程序或者系统服务向android操作系统注册一个BroadcastReceiver(广播接收器),并在这个BroadcastReceiver
中设置它想要接收的广播。这样子,一旦系统中出现了这样一个或一些广播(广播可以来自它自己或者其他应用程序),
就会查找是否有应用程序注册了receiver,如果有就通知它们,否则就什么都不做。
註冊廣播有種方式,一種是在代碼裏面,另外一種是在AndroidManifest.xml裏面。
一般推薦後者,因為易於代碼維護,有系統幫我們注冊和註銷。
下面使用代碼註冊和註銷廣播。
而Service的用途又是什麽?常常用来在后台进行一些长时间的操作,例如播放音乐,音频解码,下载数据等等。
參考代碼:
public class MyService extends Service {
//这一个是Service的接口,也是一个抽象方法
@Override
public IBinder onBind(Intent intent) {
return null;
}
//以下这些是Service的生命周期方法
//Service被创建
@Override
public void onCreate() {
// Code here
super.onCreate();
}
//Service被启动
@Override
public void onStart(Intent intent, int startId) {
/* 注册广播接收器 */
serviceReceiver = new ServiceReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(".MyService");
//可以add多個action
registerReceiver(serviceReceiver, filter);
super.onStart(intent, startId);
}
//Service被销毁
@Override
public void onDestroy() {
/* 取消注册的serviceReceiver*/
this.unregisterReceiver(serviceReceiver);
super.onDestroy();
}
//Service被暂停,挂起
public void onPause() {
//Code here
}
/* 繼承BroadcastReceiver,編寫自己的广播接收器 */
class ServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//code here, do what u wanna do
}
}
}
}
另:在xml文件中註冊廣播如下。(將ServiceReceiver獨立成1個類)
<receiver android:name=".ServiceReceiver">
<intent-filter>
<action android:name=".MyService" />
<!-- 可以有多個 -->
</intent-filter>
</receiver>
分享到:
相关推荐
这个名为"Android-Service-and-Broadcast-example-application"的项目旨在通过实例展示如何在Android中有效地使用这两种组件。以下是关于Android服务和广播接收器的关键知识点: **服务(Service)** 1. **定义**:...
Our Example Service: RefreshService Pulling Data from Yamba Summary Chapter 11 Content Providers Databases on Android Status Contract Class Update RefreshService Content Providers Creating a Content ...
Digital cellular telecommunications system (Phase 2+) Use of Data Terminal Equipment - Data Circuit ...Short Message Service (SMS) and Cell Broadcast Service (CBS) (GSM 07.05 version 7.0.1 Release 1998)
As a result, the satellite network plays an important role to extend the internetwork such as the television broadcast and Internet connection to the place where cable and terrestrial radio cannot ...
5–8 FM Broadcast Technical Standards and Digital FM Broadcasting Digital FM Broadcasting, 351 5–9 Binary Modulated Bandpass Signaling 353 On-Off Keying (OOK), 353 Binary Phase-Shift Keying (BPSK), ...
在Andriod Studio中使用Broadcast实现了简易音乐播放器的功能。可以进行播放、暂停、歌曲切换的功能。效果如下图: 由于无法上传视频,故展示GIF,没有音乐播放 项目源码 已发布至gitee 准备工作 新建assets文件夹,...
Creating Intent Filters and Broadcast Receivers Chapter 6: Using Internet Resources Downloading and Parsing Internet Resources Using the Download Manager Using Internet Services Connecting to Google ...
在"Broadcast Receivers and Permissions"这个主题中,我们将深入探讨如何使用Kotlin来实现和管理BroadcastReceiver,以及权限的相关知识。 1. **BroadcastReceiver的基本概念** - BroadcastReceiver是Android四大...
### 广播与组播服务在宏分集干扰消除下的性能研究 #### 摘要与背景 本文探讨了在cdma2000 1xEV-DO移动通信系统中,通过采用基于宏分集的干扰消除算法来提高广播和组播服务(BCMCS)的性能。随着多媒体数据服务需求的...
Broadcast/Multicast (BMC) Protocol 246 Radio Resource Control Protocol 247 RRC Functions 247 Management of RRC Connections 249 Handover 250 Summary 254 References 256 General Systems Descriptions 256 ...
根据提供的文件信息,“MPEG-2 Digital Broadcast Pocket Guide.pdf”主要涵盖了MPEG-2编码标准在数字广播领域的应用。下面将详细介绍MPEG-2的基本概念、压缩技术以及传输机制,并结合数字广播业务进行阐述。 ### ...
Chapter 5, Building a Wallet Service, explains how to build a wallet service that users can create and manage Ethereum Wallets easily, even offline. We will specifically use the LightWallet library to...
Chapter 5, Building a Wallet Service, explains how to build a wallet service that users can create and manage Ethereum Wallets easily, even offline. We will specifically use the LightWallet library to...
其中包括Activity Manager、Content Provider、Intent、Broadcast Receiver、Service等关键组件,它们定义了应用程序的生命周期和交互方式。 6. **应用层**:这是用户最直观接触的部分,包含各种预装和第三方应用,...
AlarmManager and the Scheduled Service Pattern PowerManager and WakeLocks JobScheduler Accessing Location-Based Services The Fused Location Provider Working with the Clipboard Telephony Working With ...
Broadcast Receivers,用于接收系统广播事件;以及Service Components,用于后台运行的服务。 #### 二、Android机能分析 **Start-up Walkthrough(启动流程)** - Android系统的启动过程非常复杂,涉及到多个层次...
AlarmManager and the Scheduled Service Pattern PowerManager and WakeLocks JobScheduler Accessing Location-Based Services The Fused Location Provider Working with the Clipboard Telephony Working With ...
9.1.2 Comparison of TDD and WLAN System and Service Attributes 231 9.1.3 Performance of 802.11b WLAN Systems 233 9.1.4 Comparison of UMTS TDD and 802.11b WLAN System Performance 235 9.1.5 Deployment ...
3. **基本组件**:详细解释Activity(活动)、Service(服务)、Broadcast Receiver(广播接收器)和Content Provider(内容提供者)这四大组件的工作原理和使用方法。 4. **用户界面设计**:介绍XML布局语言和...