突破Android O 系统对Service的限制

接着上一篇《android O 对Service的限制【Background Execution Limits】》内容。

我们将解决下面两个问题,并提出一个有趣的问题。

  • app处在空闲期(idle)时,如何启动Service?
  • 在App进入空闲期(idle)时,如何让Service不被立即回收?

1. App处于空闲期时,如何启动一个Service?

这种方式启动Service,将不受系统限制。

activity.getApplication().bindService(intent, connection, Service.BIND_AUTO_CREATE);



2. 在App进入空闲期时,如何让Service不被立即stop?

前面我们提到,普通的Service,在App进入空闲期时,将会被系统回收。

我们有一个修饰词 “普通的” Service。

那什么样的Service是不普通的呢?换句话来说,什么样的Service,在APP进入空闲期时不会被stop。

Application.bindService()绑定的Service。
对你没看错,一定要是Application对象绑定上的Service才不会被Stop。
一定要是Application对象bindService()

一定要是Application对象bindService()

一定要是Application对象bindService()

如果是仅仅是被Activity对象绑定的话,APP进入空闲期后,Service依然都会被stop。

    Intent in = new Intent(MainActivityV2.this, Service4.class);
    activity.getApplication().bindService(intent, connection, Service.BIND_AUTO_CREATE);



3. bind的方式启动Service依然存在缺陷

我们无法通过Intent与Service通信。
例如:以前我们可以通过 Context.startService(intent) 发送Inent给Service.
显然现在我们不能调用 通过Context.startService(intent) 发送Inent给Service.

  • 那么我们应该如何发送Intent给Service,来实现与Service的通信呢?
    这是一个值得思考的问题。




那么问题来了,我们应该如何发送Intent给Service,来实现与Service的通信呢?

下一篇一起来讨论这个有趣的问题。

原文链接:https://juejin.im/post/5dd16fb75188254eec44139c



点这里,查看大叔的博客大纲







​ 赞美是一种美德,点个赞 再走啊,老铁

weixin_2dcode

猜你喜欢

转载自blog.csdn.net/jiese1990/article/details/103134153