Service为Android四大组件之一,和Activity同样,都是Context的子类,只是它没有界面,Service很适合去执行那些长时间运行又不须要和用户交互的任务。因为Service自己是在主线程运行的,因此若是须要执行耗时操做仍是须要另外开启子线程,不然会出现ANR错误。java
Service包含三种类型:服务器
Service 有两种启动方式,startService() 和 bindService()。多线程
startService() 启动的生命周期如上图左边所示。特别的,会回调 onStartCommand()方法异步
bindService()启动的生命周期如上图右边所示。特别的,会调用 onBind() 和 onUnbind()ide
onStartCommand() 方法返回一个整数值,让系统在系统杀死此 Service 的时候如何处理此 Serviceui
不可交互的后台服务的启动方式为 startService(),this
public class MyService extends Service{
public static final String TAG = "MyService";
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.e(TAG,"onBind");
return null;
}
@Override
public void onCreate() {
Log.e(TAG,"onCreate");
super.onCreate();
runAfterStop();
}
private void runAfterStop(){
new Thread(){
@Override
public void run() {
try {
Thread.sleep(5000);
Log.e(TAG,"stopSelf()");
stopSelf();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG,"onStartCommand");
return START_NOT_STICKY;
}
@Override
public boolean onUnbind(Intent intent) {
Log.e(TAG,"onUnbind");
return super.onUnbind(intent);
}
@Override
public void onDestroy() {
Log.e(TAG,"onDestroy");
super.onDestroy();
}
}
复制代码
启动服务spa
public class BaseActivity extends AppCompatActivity{
...
Intent intent = new Intent(this, MyService.class);
startService(intent);
...
}
复制代码
前台服务会在通知栏/状态栏上显示,而且此时的 Service 的优先级比较高,线程
public class ForegroundService extends Service{
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
//id不能为0,不然通知栏/状态栏不会显示
int id = 1;
startForeground(id,createNotification());
}
private Notification createNotification(){
Intent notifiIntent = new Intent(this, FirstActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,notifiIntent,0);
Notification.Builder builder = new Notification.Builder(this)
.setContentTitle("title")
.setContentText("Content")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentIntent(pendingIntent);
return builder.build();
}
}
...
//启动服务
public class BaseActivity extends AppCompatActivity{
...
Intent intent = new Intent(this, MyService.class);
startService(intent);
...
}
复制代码
若是要取消前台服务,能够调用 stopForeground(boolean removeNotification) ,代理
参数 removeNotification 为 true 表示取消前台服务通知也移除掉通知栏/状态栏的图标,false 表示不移除
调用此方法不会致使服务中止,只是把前台转到后台
当服务中止的时候,通知栏/状态栏的图标也会同时被移除
绑定服务(Bound Services)为客户端-服务器模式。因为须要交互,因此须要有一个中间代理对象,此对象须要服务端建立而后返回给客户端持有,类型为 Binder,由 onBind() 进行返回。
服务端代码:
public class BoundService extends Service {
private IBinder mBinder = new LocalBinder();
public class LocalBinder extends Binder{
public int add(int a,int b){
return BoundService.this.add(a,b);
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
private int add(int a,int b){
return a + b;
}
}
复制代码
客户端代码:
public class BoundActivity extends Activity{
//持有绑定服务返回的 Binder 对象
private BoundService.LocalBinder mBinder;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
serviceBind();
}
@Override
protected void onStop() {
super.onStop();
unbindService(mServiceConn );
}
//绑定服务
private void serviceBind() {
Intent intent = new Intent(this, MyService.class);
bindService(intent, mServiceConn, Context.BIND_AUTO_CREATE);
}
//点击事件
public void addClick(View view){
add(2,3);
}
//调用绑定服务的方法
private void add(int a,int b){
Log.(TAG,mBinder.add(a,b));
}
//绑定服务后的回调接口
private ServiceConnection mServiceConn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.e(TAG,"bing Service Connected");
mBinder = (BoundService.LocalBinder) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.e(TAG,"unBind Service disConnected");
mBinder = null;
}
};
}
复制代码
这个只是基于简单相同进程内调用,若是在 Manifest 文件中给 BoundService 添加上 process=":remote" 变成远处服务,则上面的代码就会报错,抛出 java.lang.ClassCastException 异常,由于此种状况下返回的是 Binder 的代理对象 BInderProxy,因此抛出类型转换错误。进程间的通讯要用到 AIDL ,这个只能另外写篇回顾了。
IntentService 是 Service 的子类,用来处理异步请求 特色: 一、会建立一个默认的工做子线程处理全部的请求 二、会有一个队列逐个处理全部的 Intent 并会在 onHandleIntent() 实现 三、当 Service 处理全部的工做后会自动结束 Service,不须要手动调用 stopSelf() 四、默认实现返回值为 null 的 onBind() 方法 五、默认实现 onStartCommand(),会把请求的 Intent 放到工做队列里
因此咱们不须要去管理 IntentService 的生命周期和管理线程。而且 IntentService 在处理完因此任务后会自动关闭。当业务不须要涉及到多线程任务时,IntentService 就可以知足大多数的需求了。
实现 IntentService 的方式很简单,只须要提供一个构造方法和实现 onHandleIntent() 方法
public class TestIntentService extends IntentService {
publicTestIntentService() {
super("TestIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
//模拟耗时操做
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
复制代码
IntentService 的是使用 HandlerThread + Handler 来实现的。HandlerThread 为Thread的子类,Handler 运行在 HandlerThread 线程中,处理耗时操做。
Service 是 Android 的一种机制,运行在主线程中,若是进行耗时操做,须要建立一个子线程执行。
Service 的优先级高于后台挂起的 Activity 和其所建立的子线程 Thread。系统可能会在内存不足的时候,优先杀死后台挂起的 Activity 或 Thread,而不会轻易杀死 Service。
Thread 的运行是独立于 Activity 的,当 Activity 被 finish 掉的时候,若是没有主动中止 Thread 或未执行完任务,那么它还会继续执行。此时程序将再也不持有这个 Thread 的引用,此时将控制不了此 Thread。
因此当须要长期稳定的在后台运行某个任务时,须要使用 Service,而当 Service 在运行这个任务时要处理耗时操做时,要另外建立子线程来执行。