Android 支持服务的概念。服务是在后台运行组件,没有用户界面。可以将这些组件想象为Windows 服务或UNIX服务。 与这些服务类型类似,Android服务始终可用,但无需主动执行某些操作。
Android 支持两种服务类型的服务:本地服务和远程服务。本地服务无法供在设备上运行其他应用程序访问。一般而言,这些服务类型仅支持承载该服务的应用程序。而对于远程服务,除了可以承载服务的应用程序访问,还可以从其他应用程序访问。远程服务使用 AIDL(Android Interface Definition Language, Android接口定义语言)向客户端定义其自身。
Android 中的服务
通过查看 android.app.Service的公共方法,我们可以更深入地理解服务的概念。
Application getApplication();
Abstract IBinder onBind(Intent intent);
void onConfigurationChanged(Configuration newConfig);
void onCreate();
void onDestroy();
void onLowMemory();
void onRebind(Intent intent);
void onStart(Intent intent, int startId);
boolean onUnbind(Intent intent);
final void setForeground(Boolean isForeground);
final void stopSelf();
final void stopSelf(int startId);
final boolean stopSelfResult(int startId);
getApplication()方法返回实现服务的应用程序。
onBind()方法为在同一设备上运行的外部应用程序提供一个接口来与服务通信,此方法在远程服务中特别重要。onConfigurationChanged();支持服务在设备配置更改时重新配置自身。
系统在首次创建服务时调用onCreate(),然后才调用onStart()。此过程类似创建一个Activity的过程,在启动时执行一次初始化。例如,如果创建后台线程,可以在onCreate()方法中进行,在确保在onDestroy()中停止该线程。系统调用 onCreate(),然后调用 onStart(), 最后在关闭服务时调用 onDestroy()。onDestroy()方法为服务提供了一种机制,在关闭之前执行最后的清理。
请注意,onStart()、onCreate()、和 onDestroy()都是由系统调用的,不应该直接调用它们。而且,如果在服务类中重写任何on*()方法,请确保从这些方法调用了超类的方法版本。stopSelf()的各种版本为应用程序提供了一种机制来停止服务。客户端也可以调用 Context.stopService()来停止服务。
Android支持服务的概念有2个原因。第一,简化后台任务的实施,这种服务也就是本地服务;第二,在同一设备上运行的应用程序之间执行进程通信,这种服务也就是远程服务。本地服务与远程服务的一些重要区别。具体来讲,如果服务完全只供同一进程中的组件使用(以运行后台任务),那么客户端必须调用 Context.startService()来启动该服务。这种类型的服务为本地服务,因为它的一般用途是运行承载服务的应用程序的后台任务。如果服务支持onBind()方法,那么它属于远程服务,可通过进程间通信 (Context.bindService())进程调用。我们也将远程服务称为AIDL 支持服务,因为客户端使用 AIDL 与服务通信。
尽管 android.app.Service接口同时支持本地服务和远程服务,但并不建议提供一种服务的实现同时支持两种类型。因为每种服务的类型都有预定义的生命周期,将两种服务合并在一起可能导致错误(尽管允许这么做)。
说明:Android中的第二种服务类型有多种叫法:远程服务、AIDL服务、外部服务和RPC服务。这些名称都是指向同一类型的服务----------可供在设备上运行的其他应用程序远程访问的服务。
本地服务
本地服务由Context.startService()启动。启动以后,这些类型的服务将持续运行,直到客户端调用服务的 Context.stopService()或者服务自己调用 stopSelf()。请注意,当调用Context.startService()时,系统将实例化服务并调用服务的 onStart()方法。请记住,如果在服务启动之后(也就是服务运行时)调用 Context.startService()不会为服务创建另一个实例,但这样做将调用服务的 onStart()方法。下面给出了两个本地服务的实例。
一:定期在网络(比如Intent)上检索数据的服务 (用于上传或下载信息)。
二:任务执行程序服务,让应用程序的活动提交作业并对它们进行排队以供处理。
下面这个例子是一一个本地服务的例子 这里例子是 我们在服务的不同状态,如服务创建、服务启动、服务停止等 定义一个消息 通过状态栏来提醒用户 后台服务的不同状态。
运行效果如下
先来看看我们的Service类
BackgroundService.java
package xiaohang.zhimeng;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class BackgroundService extends Service {
private NotificationManager notificationMgr;
@Override
public void onCreate() {
super.onCreate();
notificationMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
displayNotificationMessage("starting Background Service");
Thread thr = new Thread(null, new ServiceWorker(), "BackgroundSercie");
thr.start();
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
class ServiceWorker implements Runnable {
@Override
public void run() {
// do background processing here.....
// stop the service when done...
// BackgroundService.this.stopSelf()
}
}
@Override
public void onDestroy() {
displayNotificationMessage("stopping Background Service");
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
}
private void displayNotificationMessage(String message) {
Notification notification = new Notification(R.drawable.icon, message,
System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, Activity01.class), 0);
notification.setLatestEventInfo(this, "Background Service", message,
contentIntent);
notificationMgr.notify(R.id.app_notification_id, notification);
}
}
Activity01类
package xiaohang.zhimeng;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Activity01 extends Activity {
private static final String TAG = "Activity01";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d(TAG, "starting service");
Button bindBtn = (Button) findViewById(R.id.bindBtn);
bindBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
startService(new Intent(Activity01.this,
BackgroundService.class));
}
});
Button unbindBtn = (Button) findViewById(R.id.unbindBtn);
unbindBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
stopService(new Intent(Activity01.this, BackgroundService.class));
}
});
}
}
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:id="@+id/bindBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bind"/>
<Button android:id="@+id/unbindBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UnBind"/>
</LinearLayout>
我们需要创建一个图标 并将其放在项目的drawable文件夹内,我用的项目自带的那个。需要为通知管理器NotificationManager提供一个应用程序级唯一ID(整数)。要创建唯一ID,可以向字符串资源文件 /res/values/strings.xml 添加一个项ID。当调用notify()方法时,会将唯一ID 传递给通知管理器。
<item type=”id” name=”app_notification_id”/>
最后,需要向 AndroidManifest.xml 文件添加
<service android:name=”BackgroundService”/>
标记,将其作为<application>的子标签。
BackgroundServie是承载服务的应用程序组件使用服务的一个典型例子。换言之,运行服务的应用程序也是服务的唯一使用者。因为该服务不支持其进程外的客户端,所以它是本地服务。由于本地服务不是远程服务,所以它在bind()方法中返回null。因此,绑定此服务的唯一 方法是调用Context.startService()。本地服务的重要方法包括 onCreate()、onStart()、stop()*和onDestroy()。
在BackgroundService的 onCreate()方法中,我们创建了一个线程来完成服务的主要工作。我们需要应用程序的主线程来处理用户界面,所以将服务的工作委派给一个辅助线程。另请注意,我们在onCreate()而不是onStart()中创建和启动线程。这样做是因为, onCreate()只会被调用一次,我们也希望在服务生命周期内只创建一次该线程。onStart()方法可以调用多次,所以它不符合此处的要求。我们在线程的run()方法实现中没有做任何事情,可以在这里执行HTTP调用、查询数据库等。
BackgroundService还使用NotificationManager类,以在服务启动和停止时向用户发送通知。这是本地服务将信息传递回用户的一种方式。要向用户发送通知,需要调用 getSystemService(NOTIFICATION_SERVICE)来获得通知管理器。来自通知管理器的消息将显示在状态栏中。
源码附件!
- 大小: 23 KB
- 大小: 13.6 KB
- 大小: 12.9 KB
- 大小: 19.4 KB
分享到:
相关推荐
第1章 Android操作系统概述 1 1.1 Android历史介绍 1 1.2 Android架构 1 1.3 Android平台介绍 2 1.4 现有智能手机操作系统比较 4 第2章 Android开发环境搭建 5 2.1 Eclipse和ADT插件 5 2.1.1 Eclipse安装 5 ...
概述 该项目结合 MVP 与 Clean 架构思想,探索在 Android 项目上的最佳实践。 遵循 Clean Architecture 的原则。 数据层(Data Layer):加入数据转换层(Mapper)将服务端数据模型(Entity)与本地数据模型...
第1章“Android系统概述”,概述Android系统方面的内容,包括智能手机平台的发展历程、Android系统的特点、Android的3种开发工作,以及Android的2种不同的开发方式。 第2章“Android系统开发综述”,介绍Android...
11.3 使用服务的真实示例 11.3.1 Google翻译API 11.3.2 使用Google翻译API 11.4 参考资料 11.5 小结 第12章 包 12.1 包和进程 12.1.1 包规范的细节 12.1.2 将包名称转换为进程名称 ...
13.5.2 Web Service平台概述 510 13.5.3 使用Android应用调用 Web Service 512 13.6 本章小结 524 第14章 管理Android手机桌面 525 14.1 管理手机桌面 526 14.1.1 删除桌面组件 526 14.1.2 添加桌面...
以下是我使用的几个库: //线程 // 设计组件//本地数据存储// 网络Moshi // JSON 解析器Picasso // 图片加载器Mockito // 单元测试Espresso // UI 测试架构概述: 我使用了几个外部示例来创建这个示例,主要来自这...
第1章 android系统概述 1 1.1 基础知识 1 1.1.1 android开发系统的由来 1 1.1.2 移动电话系统开发模式 2 1.1.3 未来移动电话的功能及android的优势 4 1.2 android的开发工作 6 1.2.1 android移植开发 6 1.2.2 ...
最重要的是还全面介绍了如何利用原生的C,C++(NDK)和Python、Lua等脚本语言(AndroidScriptingEnvironment)来开发Android应用,《Android应用开发揭秘》实战性强,书中的每个知识点都有配精心设计的示例,尤为...
书中不仅结合数独游戏开发案例形象生动地讲解了Android生命周期、用户界面、2D图形、多媒体,以及本地数据存储等基础知识,而且通过“Hello, Android”项目深入探讨了外部通信、基于位置的服务、内置SQLite数据库、...
TrustChain Android TrustChain Android是实现TU Delft样式区块链的本机Android应用程序,称为TrustChain。 该应用程序提供了一种易于...尽管我们已经为此存储库配置了Travis CI和CodeCov,但我们还是决定在本地计算
11.3 使用服务的真实示例 11.3.1 Google翻译API 11.3.2 使用Google翻译API 11.4 参考资料 11.5 小结 第12章 包 12.1 包和进程 12.1.1 包规范的细节 12.1.2 将包名称转换为进程名称 ...
HyperLog Android概述日志格式下载初始化使用情况获取文件中的日志使用RequestBin示例测试端点将文件推送到远程服务器示例测试端点Django HyperLog Android概述日志格式下载初始化使用情况获取文件中的日志将日志...
10.5 运行本地 Sockets示例 243 10.6 异步I/O 243 10.7 小结 244 第11章 支持C++ 245 11.1 支持的C++运行库 245 11.1.1 GAbi++ C++运行库 246 11.1.2 STLport C++运行库 246 11.1.3 GNU STL C++运行库 246 ...
关于startService的基本使用概述及其生命周期可参见《Android中startService基本使用方法概述》。 本文通过播放背景音乐的简单示例,演示startService的基本使用流程,具体内容如下 系统界面如下: 界面上面就两...
客户端SDK功能概述在使用统计服务前,开发者先要拿到本身APPID。其中AppId是客户端的身份标识,在客户端SDK初始化时使用。然后下载最新Library的 SDK压缩包,其中包括了Android SDK和AndroidDemo。Android版SDK以...
概述Meets Android 是一个原生 SDK,旨在简化 Android 移动应用程序和 Magento 商店之间的通信。 它允许您像访问本地数据一样访问 Magento 的数据。 您不必担心学习 Magento 的 SOAP 和 REST API 或处理低级细节。 ...
1.3.6. 启动本地服务 ........................ 18 1.3.7. 启动 Home Laucher ............... 19 第 2 章 第二章 ................... 21 2.1. Activity 的架构设计 ....................... 21 2.1.1. 概述 .........
概述有3个主要组成部分: 本地通知(管理布局通知) 全局通知(管理浮动通知) 通知远程(管理状态栏通知) 和2个次要组件: 公告板(保存公告列表) 通知效果(管理铃声,振动等) 每个通知可以具有自己的布局和...