在Android中,Service有两种启动方式:startService()和bindService()。
startService()启动Service时,Service会被创建并且调用onCreate()和onStartcommand()方法。Service会一直保持运行状态,直到调用stopService()或者stopSelf()方法。
bindService()启动Service时,Service会被创建并且调用onCreate()方法,但不会调用onStartcommand()方法。通过bindService()方法绑定的组件可以与Service进行通信,但是当所有绑定的组件都解绑后,Service会自动销毁。
因此,startService()方式启动的Service是一种独立的组件,可以在后台持续运行执行任务,而bindService()方式启动的Service则是一种与组件绑定的服务,只有在绑定的组件存在时才会运行。
本文讲述的是与组件绑定的Service的通信
全部的项目结构放在这里主要讲述的是bindService
package com.example.servicetest;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import com.example.servicetest.Service.MyBindService;
import com.example.servicetest.Service.myService;
public class MainActivity extends AppCompatActivity {
private MyBindService.MyBinder mBinder = null;
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBinder = (MyBindService.MyBinder)service;
mBinder.test();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startService(View view) {
Intent intent = new Intent(this, myService.class);
startService(intent);
}
public void bindService(View view) {
Intent intent = new Intent(this, MyBindService.class);
bindService(intent,conn,BIND_AUTO_CREATE);
}
}
我们这里讲述一下ServiceConnection这个方法,这个方法是用于处理Service的连接和断开的,需要重写两个方法,见名知意我就不分辨了.
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mBinder = (MyBindService.MyBinder)service;
mBinder.test();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
这里的service需要传入的是一个IBinder的类型,所以我们需要对类型进行强制转换,我们不需要对IBinder这个接口进行单独的实现,因为通过在Service中写内部类继承Binder类的方式就是对IBinder接口的实现.
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind: ");
return new MyBinder(this);
}
绑定返回BInd对象,用于Service和Activity之间进行通信
package com.example.servicetest.Service;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
public class MyBindService extends Service {
public void test(){
Log.d(TAG, "test: 这是");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
Log.d(TAG, "onBind: ");
return new MyBinder(this);
}
private static final String TAG = "MyBindService2";
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate: ");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand: ");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
@Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "onUnbind: ");
return super.onUnbind(intent);
}
public class MyBinder extends Binder{
private MyBindService myBindService;
public MyBinder(MyBindService bindSercive) {
myBindService = bindSercive;
}
public void test(){
Log.d(TAG, "test: ");
myBindService.test();
}
}
}
Service中的几个重写的方法就不讲述了,见名知意即可,我们主要讲述的是Service中的这个内部类
public class MyBinder extends Binder{
private MyBindService myBindService;
public MyBinder(MyBindService bindSercive) {
myBindService = bindSercive;
}
public void test(){
Log.d(TAG, "test: ");
myBindService.test();
}
}
这里的MyBinder继承Binder实现了IBinder接口的实现,MyBinder方法传入的是MyBindService的对象,这里是为了实现内部类和Service的关联,从而能在内部类中调用Service中的方法,这样子就能实现,多层的调用了.
标签:service,Service,void,绑定,import,Android,public,MyBinder,MyBindService From: https://blog.csdn.net/2301_81869574/article/details/139712873