Android O后台执行限制未完全应用于有界和已启动的服务

时间:2017-08-18 09:57:31

标签: android android-service android-service-binding android-8.0-oreo

我的示例应用使用a

我有一个简单的服务,它使用以下代码片段启动和限制:

targetSdkVersion 26

请注意,我使用全局应用val intent = BoundServiceTest.buildIntent(applicationContext) applicationContext.startService(intent) applicationContext.bindService(intent, serviceConnection, BIND_AUTO_CREATE) 进行绑定,而不是Context

服务本身只会影响一些基本的日志记录:

Activity Context

基本上我不确定,如果Android O是否应用了后台执行限制,因为基本上文档说明了(Background Service Limitations):

  

绑定服务不受影响

     

这些规则不会以任何方式影响绑定服务。如果你的应用程序   定义绑定服务,其他组件可以绑定到该服务   你的应用程序是否在前台。

但似乎操作系统对Logcat不太确定:

class BoundServiceTest : Service() {

  companion object {
    private val TAG = "BoundServiceTest"

    fun buildIntent(context: Context): Intent {
        return Intent(context, BoundServiceTest::class.java)
    }
  }

  private val binder = Binder()

  override fun onBind(p0: Intent?): IBinder {
    Log.d(TAG, "onBind")
    return binder
  }

  override fun onUnbind(intent: Intent?): Boolean {
    Log.d(TAG, "onUnbind")
    return super.onUnbind(intent)
  }

  override fun onCreate() {
    super.onCreate()
    Log.d(TAG, "onCreate")
  }

  override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    Log.d(TAG, "onStartCommand: startId = " + startId)
    return START_STICKY
  }

  override fun onDestroy() {
    Log.d(TAG, "onDestroy")
    super.onDestroy()
  }

}

de.continental.android.androidoservicetest D/BoundServiceTest: onCreate de.continental.android.androidoservicetest D/BoundServiceTest: onStartCommand: startId = 1 de.continental.android.androidoservicetest D/BoundServiceTest: onBind ? W/ActivityManager: Stopping service due to app idle: u0a141 -1m9s733ms de.continental.android.androidoservicetest/.BoundServiceTest I/RunningState: Unknown non-service process: de.continental.android.androidoservicetest #14943 I/chatty: uid=1000(system) RunningState:Ba identical 1 line I/RunningState: Unknown non-service process: de.continental.android.androidoservicetest #14943 日志消息表明服务应该被停止(无论它是绑定服务),但操作系统不会停止我的服务:调用ActivityManager方法的日志消息是与没有绑定的简单启动服务相比,没有显示。

如何在Android O中处理此方案(启动和绑定服务)?或者我遇到错误?

0 个答案:

没有答案