什么是服务
顾名思义就是"服务"后台运行,可交互这样的一个东西。它跟Activity的级别差不多,但是他不能自己运行,需要通过某一个Activity或者其他Context对象来调用,这里要说明一下的是如果你在Service的onCreate或者onStart做一些很耗时间的事情,最好在Service里启动一个线程来完成,因为Service是跑在主线程中,会影响到你的UI操作或者阻塞主线程中的其他事情。
那我们什么时候会用到Service呢?比如我们播放音乐的时候,有可能想边听音乐边干些其他事情,当我们退出播放音乐的应用,如果不用Service,我们就听不到歌了,所以这时候就得用到Service了,又比如当我们一个应用的数据是通过网络获取的,不同时间(一段时间)的数据是不同的这时候我们可以用Service在后台定时更新,而不用每打开应用的时候在去获取。
创建一个Service
- 在项目中定义一个服务,新建一个Service项目 New - Service - Service。
生成代码如下:Myservice继承自Service类,onBind方法也别醒目,这个方法是Service中唯一一个抽象方法,所以必须要在子类中实现
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
- 其中还可重写三个方法:
onCreate()方法是服务创建的时候调用,一个服务创建结束后只调用一次该方法。
onStartCommand()方法会在每次服务启动的时候调用,如果我们希望服务一旦启动就立刻去执行某个动作,就可以将逻辑写在其中。
onDestory()会在服务销毁的时候调用,在其中回收那些不再使用的资源。
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
}
- 服务的启动停止方法主要借助Intent来实现
Intent startIntent = new Intent(this,MyService.class);
startService(startIntent);//启动服务
Intent stop = new Intent(this, MyService.class);
stopService(stop); //停止服务
实践
下面将利用服务实现一个计数器的功能,并且程序退出到后台时仍然在计数
- 在onStartCommand()中添加代码:
//MyService内的私有变量
private boolean isWork = true;
private static int time = 0;
new Thread(new Runnable() {
@Override
public void run() {
while (isWork) {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
final Notification notification = new NotificationCompat.Builder(getApplication())
.setContentTitle("标题")
.setContentText(time+"")
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentIntent(pi)
.build();
startForeground(1, notification);
time++;
Log.d("TAG", "run: " + time);
}
time=0;
}
}).start();
该代码在onStartCommand()中开启了一个线程,并在线程中创建一个通知,并且调用startForeground()方法使该通知成为前台服务,一直保持在通知栏显示。
- 在onDestroy()方法中添加代码
isWork = false;
停止线程的进行
- 在主方法中添加代码:
Button start = findViewById(R.id.start);
Button stop = findViewById(R.id.stop);
//开启服务
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent statrt = new Intent(MainActivity.this, MyService.class);
startService(statrt);
}
});
//停止服务
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent stop = new Intent(MainActivity.this, MyService.class);
stopService(stop);
}
});