Call the BroadCast receiver of the main Android activity

I want to start broadcast receiver class from my app’s main activity. Main activity has all the GUI stuff, this activity starts a service which performs all the work. For making this service start after reboot, I have written a broadcast receiver class. Now I am confused where to call broadcast receiver class.

  • How can I call it from my main activity (I don’t want to make main activity a broadcast reciver)?
  • If I call broadcast receiver from main activity, will it create a new instance of the broadcast class every time the activity starts. If so, will it cause memory leaks or the previous broadcast receiver instance will automatically be destroyed?

Regards

As a solution you may apply your own intent filter to BroadcastReceiver
, register it and then call sendBroadcast()
in your activity just after registering the receiver. This suppose that you compare intents which comes to your receiver inside of onReceive()
method.

What about memory, it depends on how do you manage your activity’s lifecycle, because in a case of remaining references to your activity it will not be removed from the memory and so will cause creation of multiple instances of your receiver.

And you also may add android.intent.action.BOOT_COMPLETED
intent filter to start your receiver after device’s booting

Hello, buddy!稿源:Hello, buddy! (源链) | 关于 | 阅读提示

本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 移动开发 » Call the BroadCast receiver of the main Android activity

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录