简介
结束服务的用法,服务的生命周期,前台服务通知,IntentService
定义一个服务
New -> Service -> Service
Exported 属性表示是否允许除了当前程序之外的其他程序访问这个服务;
Enabled 属性表示是否启用这个服务,默认 true 。如设置 false ,将不可用;
onCreate() 服务创建的时候调用; onStartCommand() 在每次服务启动的时候调用; onDestroy() 在服务销毁的时候被调用。
启动和停止服务
//启动,当连续启动时,onCreate 只会在第一次时调用
Intent intent = new Intent(this, MyService.class);
startService(intent);
//停止
Intent intent = new Intent(this, MyService.class);
stopService(intent);
活动和服务通信
利用 onBind 方法;
在 Service 中创建 DownloadBinder 继承 Binder,并在 onBind 方法中返回该 binder;
private DownloadBinder mBinder = new DownloadBinder();
class DownloadBinder extends Binder {
public void startDownload() {
Log.d(TAG, "startDownload: ");
}
public int getProgress() {
Log.d(TAG, "getProgress: ");
return 0;
}
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
在 Activity 中创建 ServiceConnection 匿名类,重写 onServiceConnected 和 onServiceDisconnected 方法,这两个方法分别在成功绑定以及解除绑定的时候调用。
private MyService.DownloadBinder downloadBinder;
private ServiceConnection connection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
downloadBinder = (MyService.DownloadBinder) iBinder;
downloadBinder.startDownload();
downloadBinder.getProgress();
}
@Override
public void onServiceDisconnected(ComponentName componentName) { }
};
//将服务和活动进行绑定
Intent intent = new Intent(this, MyService.class);
bindService(intent, connection, BIND_AUTO_CREATE);
//解除绑定
unbindService(connection);
服务的生命周期
需要注意的地方:
- 第一次调用 startService 会执行 onCreate、onStartCommand,后续调用 startService 只会执行 onStartCommand。
- 不管调用多少次 startService,服务都只会存在一个实例,所以只需要调用一次 stopService 或 stopSelf 服务就会停止。
- 通过 bindService 方法会执行 onBind,如果服务之前没有创建,则会先执行 onCreate,当执行完 onBind,调用方即可获取到 IBinder 实例与服务通信。
- 调用 startService 之后再调用 stopService,会执行 onDestroy;调用 bindService 之后再调用 unbindService,会执行 onDestroy;
- 如果同时调用了 startService 和 bindService,则需要同时调用 stopService 和 unbindService 才会执行 onDestroy;
前台服务
多数的服务都是在后台运行的,所以优先级是比较低的,当系统内存不足的时候,后台服务就可能会被回收。使用前台服务可以避免这点。
前台服务会有一个正在运行的图标在状态栏显示,下拉状态栏后可以看到更加详细的信息。
//创建前台可见服务
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationChannel channel = new NotificationChannel("id", "name",
NotificationManager.IMPORTANCE_LOW);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
Notification notification = new NotificationCompat.Builder(this, channel.getId())
.setContentTitle("This is content title")
.setContentText("This is content text")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.build();
startForeground(1, notification);
IntentService
服务中的代码都是默认运行在主线程中的,如果在服务中处理耗时的逻辑,容易出现 ANR;
利用 IntentService 可以简单的创建一个异步的、会自动停止的服务;
public class MyInterService extends IntentService {
private static final String TAG = "MyInterService";
public MyInterService() {
super(TAG);
}
/**
* 处理具体逻辑,在子线程中运行,在运行完之后调用 onDestroy
*/
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "onHandleIntent: " + Thread.currentThread().getId());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
}