【起航计划 033】2015 起航计划 Android APIDemo的魔鬼步伐 32 App->Service->Foreground Service Controller service使用,共享

Android系统也提供了一种称为“Service”的组件一般在后台运行。Activity 能够用来启动一个Service,Service启动后能够保持在后台一直运行,即便启动它的Activity退出或是切换到别的应用Service也能保持运行状态。android

Service 能够以两种形式存在:app

  • Started 当一个如Activity使用startService()来启动一个Service,一旦Service启动后,就不受启动它的Activity控制, 能够在后台长期运行,一般这种Service在后台执行某个费时操做(以下载文件)不会向启动它的Activity返回结果。
  • Bound 为Activity或是其它程序部件使用bindService()来启动Service。Bound Service提供了一种Client/Service方法容许调用Service的Activity与Service进行交互:发送请求,取得结果,并 支持进程间通讯。 通常Bound Service的生命周期和启动它的Activity相同,多个Activity能够同时绑定一个Service。 当全部Activity 都断开与Service之间的绑定时。Service自动结束。

虽然Service能够有上述两种表现形式,这只是为了说明上的方便,实际上同一个Service能够同时以两种方式存在,只要实现两种方法所定义的接口函数就能够了。ide

建立一个Service,首先须要定义一个Service的子类,而后根据须要重载Service定义的一些核心方法:函数

  • onStartCommand() 当一个Activity调用startService时,Android系统会调用Service的onStartCommand()方法, 前面说过使用startService启动的Service会在后台长期运行,不受启动它的Activity控制,所以应用程序有责任来中止 Service,Service也能够调用stopSelf来中止自身。
  • onBind() 当一个Activity 使用bindService()来绑定Service时Android系统会调用Service的onBind方法,onBind须要返回一个IBind 对象给调用者(Client)。Client而后可使用IBind提供的方法来使用Service。
  • onCreate() Service第一次建立时被调用,和Activity的onCreate相似。
  • onDestroy() Service退出时调用。

定义了Service类和实现相应方法后,和Activity同样,也须要在AndroidManifest.xml中定义这个Service:this

<manifest … ><application … >
<service android:name=”.ExampleService” />< /application>
< /manifest>

和Activity同样,也能够为Service定义Intent Filter,若是你不想共享这个Service,能够将android:exported属性定义为false。spa

一般状况下Service在后台运行,当Android也支持Service运行在前台,运行在前台的Service必须在屏幕顶端的Status Bar提供一个Notification以提示用户有Service在运行。好比提供个Media Player使用的Service运行在前台,而在标题栏显示当前曲目。设计

 

本例Foreground Service Controller就显示了一个在前台运行的Service, 前台运行的Service能够经过调用startForeground()使Service在前台运行。stopForeground中止前台运行,但 Service自己不会中止。 startForeground,stopForeground是从2.0开始支持的,以前的版本采用setForeground。code

本例为了支持2.0以前和2.0以后的版本,采用了Reflection的方法来来查找当前版本是否含有startForeground和stopForeground,若是有则调用,没有则仍是使用setForeground。orm

若是找到的话,如下的变量用来存储startForeground和stopForeground方法。和本例Service不相关,就不详述了。 只要知道startForegroundCompat 和stopForegroundCompat的功能就是startForeground 和stopForeground就好了。xml

private static final Class[] mStartForegroundSignature = new Class[] {
 int.class, Notification.class};
private static final Class[] mStopForegroundSignature = new Class[] {
 boolean.class};
 
private Method mStartForeground;
private Method mStopForeground;
private Object[] mStartForegroundArgs = new Object[2];
private Object[] mStopForegroundArgs = new Object[1];

 

下面来看看ForegroundService的代码:
首先是必须做为Service的子类:

public class ForegroundService extends Service

由于是做为“Started” Service来设计的,所以需定义onStartCommand ,一样onStartCommand也是在Android 2.0以后添加的,2.0以前为onStart。本例为了支持全部版本,两个方法对实现了,对应2.0以后的版本,只会调用 onStartCommand,2.0以前的只会调用onStart:

// This is the old onStart method that
// will be called on the pre-2.0 platform.
// On 2.0 or later we override onStartCommand() so this
// method will not be called.
@Override
public void onStart(Intent intent, int startId) {
handleCommand(intent);
}
 
@Override
public int onStartCommand(Intent intent,
 int flags, int startId) {
handleCommand(intent);
// We want this service to
//continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}

 

 

onStartCommand 能够有返回结果,这个返回值告诉Android系统当这个Service被Kill以后(好比当系统内存不足时)后续操做。START_STICKY 表示系统Kill这个Service以后,若是从新建立这个Service时在调用onStartCommand ,不会将最后的Intent做为参数传入,也就是说intent=null. START_REDELIVER_INTENT则会传入被杀前未处理的最后一个Intent。

本Service不做为Bind Service ,所以经过一个空实现:

@Override
public IBinder onBind(Intent intent) {
  return null;
}

 

 最后看看如何启动/中止这个Service, Controller 是做为这个Service的控制类来实现的,提供了前台启动,后台启动,和中止Service操做:

private OnClickListener mForegroundListener
 = new OnClickListener() {
 public void onClick(View v) {
 Intent intent
 = new Intent(ForegroundService.ACTION_FOREGROUND);
 intent.setClass(Controller.this,
 ForegroundService.class);
 startService(intent);
 }
};
 
private OnClickListener mBackgroundListener
 = new OnClickListener() {
 public void onClick(View v) {
 Intent intent
 = new Intent(ForegroundService.ACTION_BACKGROUND);
 intent.setClass(Controller.this,
 ForegroundService.class);
 startService(intent);
 }
};
 
private OnClickListener mStopListener = new OnClickListener() {
 public void onClick(View v) {
 stopService(new Intent(Controller.this,
 ForegroundService.class));
 }
};

 

 

相关文章
相关标签/搜索