Service介绍
1.Service,服务,是四大组件之一, 和Activity 非常相似, 一般运行在后台, 没有用户界面, 可执行的程序
2.Activity 和 Service的区别
(1)不同点: Activity : 可以和用户交互, 页面可见
Service : 后台运行, 没有界面
(2)相同点: 在清单文件中注册, 都有自己的生命周期
Service特点
service在后台运行,不用与用户进行交互。即使应用退出,服务也不会停止。
当应用进程被杀死时,服务便会停止.
service运行在主线程中,当需要执行耗时操作的时候,需要在服务中创建子线程完成
service 的用途:播放音乐;后台下载大文件等
如何创建Service
1, 定义一个类, 继承Service
2, 重写父类的方法, onBind() — 必须重写的方法
3, 在清单文件中, 注册Service
Service的启动方式以及生命周期
启动方式一 (startService)
1,右键创建一个Service ,会自动在清单文件中创建
public class MyService extends Service {
private static final String TAG = "MyService";
public MyService() {
Log.i(TAG, "MyService: 创建");
}
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind: 绑定");
return null;
}
}
xml布局文件中设置两个按钮,一个启动,一个停止
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/create_server_id"
android:text="启动一个服务"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/stop_server_id"
android:text="停止一个服务"
/>
</LinearLayout>
Java中的代码
public class MainActivity extends AppCompatActivity {
private Button createServerId;
private Button stopServerId;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stopServerId = findViewById(R.id.stop_server_id);
createServerId = findViewById(R.id.create_server_id);
stopServerId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopService(intent);
}
});
createServerId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(MainActivity.this, MyService.class);
startService(intent);
}
});
}
}
验证生命周期
如果服务已经开启,就不会重复的执行 onCreate() ,而是调用 onStart() 或 onStartCommand()。而服务停止的时候调用 onDestory()
一旦服务开启跟开启者就没有任何关系;
开启者退出之后,服务还是可以在后台长期运行的。前提是没有调用 stopService(Intent);
开启者不能调用服务里面的方法
启动方式的生命周期如下:
启动方式:onCreate() — onStartCommand() — onDestroy()
开启服务:startService()
停止服务:stopService()
启动方式二 (bindService)
1,定义一个类,并继承 Service
2,在 AndroidManifest.xml 中配置此 Service
3,使用 Context 的 bindService(Intent, ServiceConnection, int) 方法来启动此 Service
4,不使用该服务时,调用 unbindService(ServiceConnection) 方法停止此 Service
绑定服务不会调用 onStart() 或 onStartCommand() 方法。
注意:同一个服务, 只能被同一个启动源, 绑定一次, 除非解绑后, 可以再次绑定
特点:
bind的方式开启服务,绑定服务,调用者挂了,服务也会跟着挂掉。
绑定者可以调用服务里面的方法
bindService代码
创建服务:自定义类继承Service,返回代理人,方便开启者调用服务中的方法
public class MyService extends Service {
@Nullable
@Override
@Override
public IBinder onBind(Intent intent) {
Log.i(TAG, "onBind: ");
return new MyBinder();
}
public class MyBinder extends Binder{
public void callShow(){
show();
}
}
//服务中提供的方法
public void show(){
Log.i(TAG, "show: 我是秀儿..");
}
}
绑定服务和解除绑定
public class MainActivity extends AppCompatActivity {
private Intent intent;//开启服务的意图对象
private MyService myService;//通过绑定获取Service
//绑定服务连接
private ServiceConnection connection=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i(TAG, "onServiceConnected: ");
MyService.MyBinder binder = (MyService.MyBinder) service;
binder.callShow();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.绑定服务
intent =new Intent(this,MyService.class);
bindService(intent,connection, Service.BIND_AUTO_CREATE);
}
//2.解除绑定服务
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(connection);
}
}
绑定方式:onCreate() – onBind() — onUnbind() — onDestroy()
绑定服务:bindService()
解除绑定:unbindService()
两种的启动服务的最大特点
第一种,一旦启动,只能手动停止.
第二种,可以调用服务里的方法.
两种方式的生命周期对比图
两种启动方式切换
1,先掉用startServer 方法开启服务.(保证长期运行)
2,调用bindServer 去获取中间人对象
3,调用nubindServer解绑服务.(服务不销毁)
4,调用stopService 去停止服务.
前台服务Foreground Service
案例:https://blog.csdn.net/qq_34178710/article/details/93478931#1_2