Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. class LocalService : Service() {
  2.  
  3. private val binder = LocalBinder()
  4. private val generator = Random()
  5.  
  6. val randomNumber: Int
  7. get() = generator.nextInt(100)
  8.  
  9. inner class LocalBinder : Binder() {
  10. fun getService(): LocalService = this@LocalService
  11. }
  12.  
  13. override fun onBind(intent: Intent): IBinder {
  14. return binder
  15. }
  16.  
  17. override fun onDestroy() {
  18. super.onDestroy()
  19. LeakSentry.refWatcher.watch(this) // Only modification is to add LeakCanary
  20. }
  21. }
  22.  
  23. class MainActivity: Activity() {
  24.  
  25. private var service: LocalService? = null
  26. private val serviceConnection = object: ServiceConnection {
  27. override fun onServiceConnected(name: ComponentName?, binder: IBinder?) {
  28. service = (binder as LocalBinder).getService()
  29. }
  30. override fun onServiceDisconnected(name: ComponentName?) {
  31. service = null
  32. }
  33. }
  34.  
  35. override fun onCreate(savedInstanceState: Bundle?) {
  36. super.onCreate(savedInstanceState)
  37.  
  38. bindService(Intent(this, LocalService::class.java), serviceConnection, BIND_AUTO_START)
  39. }
  40.  
  41. override fun onDestroy() {
  42. service?.let {
  43. unbindService(serviceConnection)
  44. service = null
  45. }
  46. super.onDestroy()
  47. }
  48. }
  49.  
  50. ├─ com.example.serviceleak.LocalService$LocalBinder
  51. │ Leaking: NO (it's a GC root)
  52. │ ↓ LocalService$LocalBinder.this$0
  53. │ ~~~~~~
  54. ╰→ com.example.serviceleak.LocalService
  55. ​ Leaking: YES (RefWatcher was watching this)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement