|
|
@Override
protected void onStart(){
super.onStart();
// 绑定服务
Intent backIntent = new Intent(this, BackgroundService.class);
bindService(backIntent, mConnection, BIND_AUTO_CREATE);
startService(backIntent);
}
startService 是 Android 系统中用于启动 Service 的方法。让我详细解释一下:
startService 方法详解
方法来源
startService 是 Context 类中的方法
在测试中通过 InstrumentationRegistry.getInstrumentation().context 获取的上下文对象调用
作用
启动指定的 Service 组件
如果 Service 尚未运行,则会创建并调用 onCreate() 和 onStartCommand() 方法
如果 Service 已经运行,则只会调用 onStartCommand() 方法
参数
接收一个 Intent 对象作为参数
该 Intent 用于指定要启动的 Service 类(这里是 LlmBGService)
返回值
返回一个 ComponentName 对象
如果启动成功,返回非空值
如果启动失败,返回 null
|
|