英文:
Why BoundService is null?
问题
我真的是Android新手,尝试创建一个简单的服务。我查看了并尝试了Android文档,但无法启动服务。服务始终为null。程序没有调用boundServiceConnection
函数。
这是我的服务代码:
class BoundService : Service() {
private val localBinder: IBinder = MyBinder()
override fun onBind(intent: Intent): IBinder? {
return localBinder
}
fun randomGenerator(): Int {
val randomNumber = Random()
return randomNumber.nextInt()
}
inner class MyBinder : Binder() {
fun getService(): BoundService = this@BoundService
}
}
这是我的主活动:
var boundService: BoundService? = null
var isBound = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onStart() {
super.onStart()
Intent(this, BoundService::class.java).also { intent ->
bindService(intent, boundServiceConnection, Context.BIND_AUTO_CREATE)
}
}
fun onButtonClick(v: View) {
if (isBound) {
val num: Int = boundService!!.randomGenerator()
Toast.makeText(this, "number: $num", Toast.LENGTH_LONG).show()
}
}
override fun onStop() {
super.onStop()
if (isBound) {
unbindService(boundServiceConnection)
isBound = false
}
}
private val boundServiceConnection: ServiceConnection = object : ServiceConnection {
override fun onServiceConnected(name: ComponentName, service: IBinder) {
val binderBridge = service as MyBinder
boundService = binderBridge.getService()
isBound = true
}
override fun onServiceDisconnected(name: ComponentName) {
isBound = false
boundService = null
}
}
英文:
I'm really new at Android and try to create simple service. I checked an tried to Android Documentation
but i cannot start service. Service always null.Program is not calling boundServiceConnection
function
Here my code Service code
class BoundService : Service() {
private val localBinder: IBinder = MyBinder()
override fun onBind(intent: Intent): IBinder? {
return localBinder
}
fun randomGenerator(): Int {
val randomNumber = Random()
return randomNumber.nextInt()
}
inner class MyBinder : Binder() {
fun getService(): BoundService = this@BoundService
}
}
Here is my main activity
var boundService: BoundService? = null
var isBound = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun onStart() {
super.onStart()
Intent(this, BoundService::class.java).also { intent ->
bindService(intent, boundServiceConnection, Context.BIND_AUTO_CREATE)
}
}
fun onButtonClick(v: View) {
if (isBound) {
val num: Int = boundService!!.randomGenerator()
Toast.makeText(this, "number: $num", Toast.LENGTH_LONG).show()
}
}
override fun onStop() {
super.onStop()
if (isBound) {
unbindService(boundServiceConnection)
isBound = false
}
}
private val boundServiceConnection: ServiceConnection = object : ServiceConnection {
override fun onServiceConnected(name: ComponentName, service: IBinder) {
val binderBridge = service as MyBinder
boundService = binderBridge.getService()
isBound = true
}
override fun onServiceDisconnected(name: ComponentName) {
isBound = false
boundService = null
}
}
答案1
得分: 1
关于您的应用程序的服务组件,它应该在您的应用程序清单中进行注册。
关于绑定服务的 Android 指南 似乎没有提供任何关于所需的(假设丢失的)清单条目的信息,因为这在服务概述中有涵盖。
英文:
With regard to the service component of your app, it should be registered in your application manifest.
The android guide on bound services seems to not provide any information on the required (and assumed missing) manifest entry as it was covered in the service overview.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论