为什么 BoundService 为空?

huangapple go评论78阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年10月13日 22:50:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64337567.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定