一、startService 和 bindService 的区别
1162622-20220819104452056-1683090667.jpg
执行startService时,Service会经历onCreate->onStartCommand。当执行stopService时,直接调用onDestroy方法。调用者如果没有stopService,Service会一直在后台运行,下次调用者再起来仍然可以stopService。
执行bindService时,Service会经历onCreate->onBind。这个时候调用者和Service绑定在一起。调用者调用unbindService方法或者调用者Context不存在了(如Activity被finish了),Service就会调用onUnbind->onDestroy。这里所谓的绑定在一起就是说两者共存亡了。
多次调用startService,该Service只能被创建一次,即该Service的onCreate方法只会被调用一次。但是每次调用startService,onStartCommand方法都会被调用。Service的onStart方法在API 5时被废弃,替代它的是onStartCommand方法。
第一次执行bindService时,onCreate和onBind方法会被调用,但是多次执行bindService时,onCreate和onBind方法并不会被多次调用,即并不会多次创建服务和绑定服务。
二、startService简单用法
1、创建一个service
class MyService1 : Service() {
private var countDownTimer : CountDownTimer? = null
private val mDecimalFormat = DecimalFormat("00")
override fun onBind(intent: Intent): IBinder? {
return null
}
override fun onCreate() {
super.onCreate()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
startCountTimer((100) * 1000L)
return super.onStartCommand(intent, flags, startId)
}
override fun onDestroy() {
super.onDestroy()
}
/**
* 开启倒计时
*
* millisInFuture : 毫秒值
*/
fun startCountTimer(millisInFuture: Long){
countDownTimer = object : CountDownTimer(millisInFuture,1000){
override fun onFinish() {
Log.e("TAG","倒计时结束")
}
@SuppressLint("SetTextI18n")
override fun onTick(millisUntilFinished: Long) {
val hour = millisUntilFinished / 1000 / 60 / 60
val minute = millisUntilFinished / 1000 / 60 % 60
val second = millisUntilFinished / 1000 % 60
Log.e("TAG",getString(
R.string.live_manager_text20,
"${mDecimalFormat.format(hour)}:${mDecimalFormat.format(minute)}:${mDecimalFormat.format(second)}"))
}
}
countDownTimer?.start()
}
}
2、启动服务
startService(Intent(this@CallerActivity,MyService1::class.java))
3、注册服务
<service
android:name=".service.MyService1"
android:enabled="true"
android:exported="true"></service>
三、bindService简单使用
1、创建一个service
class BindService : Service() {
private var countDownTimer : CountDownTimer? = null
private val mDecimalFormat = DecimalFormat("00")
override fun onBind(intent: Intent): IBinder {
return MyBinder()
}
override fun onUnbind(intent: Intent?): Boolean {
return super.onUnbind(intent)
}
override fun onDestroy() {
super.onDestroy()
}
class MyBinder : Binder() {
fun printTest(){
Log.e("TAG", "printTest")
}
fun playVideo(){
Log.e("TAG", "playVideo")
// BindService().startCountTimer((100) * 1000L)
}
}
fun method1(){
Log.e("TAG", "method1")
// startCountTimer((100) * 1000L)
}
/**
* 开启倒计时
*
* millisInFuture : 毫秒值
*/
fun startCountTimer(millisInFuture: Long){
countDownTimer = object : CountDownTimer(millisInFuture,1000){
override fun onFinish() {
Log.e("TAG","倒计时结束")
}
@SuppressLint("SetTextI18n")
override fun onTick(millisUntilFinished: Long) {
val hour = millisUntilFinished / 1000 / 60 / 60
val minute = millisUntilFinished / 1000 / 60 % 60
val second = millisUntilFinished / 1000 % 60
Log.e("TAG","${mDecimalFormat.format(hour)}:${mDecimalFormat.format(minute)}:${mDecimalFormat.format(second)}")
}
}
countDownTimer?.start()
}
}
2、注册服务
<service
android:name=".service.MyService2"
android:enabled="true"
android:exported="true"></service>
3、绑定服务
public class MainActivity extends Activity {
private MyConnection conn;
MyBinder myBinder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onDestroy() {
super.onDestroy();
unbindService(conn);
}
public void start(View v){
Intent service = new Intent(this,BindService.class);
//通过绑定的方式开启service
conn = new MyConnection();
//使用bindService 开启反服务
//参数 1. intent 包含要启动的service
//2. ServiceConnection接口 通过它可以接受服务开启或者停止的消息
//3.开启服务时操作的选项 一般传入BIND_AUTO_CREATE自动创建service
bindService(service, conn, BIND_AUTO_CREATE);//绑定存在自动创建
}
public void stop(View v){
//使用bindService开启服务 要是用unbindService停止
unbindService(conn);
}
public void method(View v){
// BindService service = new BindService();
// service.method1();
myBinder.printTest();
myBinder.playVideo() ;
}
private class MyConnection implements ServiceConnection{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
//只有当service onbind方法返回值不为null 调用
Log.e("TAG", "onServiceConnected");
myBinder=(MyBinder) service;
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.e("TAG", "onServiceDisconnected");
}
}
}