Android BroadCastReceiver 滞后应用程序
我在活动中使用的 BroadcastReceiver
存在问题。我实际上正在这样做:
在 onCreate()
中:
receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("finish")) {
// some code
}
}
};
registerReceiver(receiver, intentFilter);
以及在 onResume()
和 onPause()
中,我正在这样做:
@Override
public void onResume(){
super.onResume();
MyCollectionList.this.registerReceiver(receiver, intentFilter);
}
@Override
public void onPause(){
super.onPause();
MyCollectionList.this.unregisterReceiver(receiver);
}
其中intentFilter是:
IntentFilter intentFilter = new IntentFilter("finish");
当我在 6 个活动中执行此操作时,我需要添加此广播接收器,我的应用程序开始滞后并且比以前变慢。
那么有没有其他更好的方法来观察意图过滤器而不减慢应用程序/或在我的情况下的最佳方法。
提前致谢!
I have an issue with BroadcastReceiver
which I'm using in my activities. I'm actually doing this :
In onCreate()
:
receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("finish")) {
// some code
}
}
};
registerReceiver(receiver, intentFilter);
and in onResume()
and onPause()
I'm doing this :
@Override
public void onResume(){
super.onResume();
MyCollectionList.this.registerReceiver(receiver, intentFilter);
}
@Override
public void onPause(){
super.onPause();
MyCollectionList.this.unregisterReceiver(receiver);
}
where intentFilter is :
IntentFilter intentFilter = new IntentFilter("finish");
and when I do this in 6 activities where I need to add this broadcast receiver my application start lagging and getting slow than before.
So is there any other better way to watch for intent filters without slowing the app/or best way in my situation.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要将接收器注册到 Activity 的上下文中,而是将其注册到第一个 Activity 中的应用程序上下文中,如下所示:
这样,即使您的活动进入“暂停”状态,您的接收器也将保持活动状态,因为您的应用程序将继续在后台运行。
希望这有帮助。
Instead of registering your receiver with Activity's context, register it with your application's context in your 1st activity as below:
This way even if your activities goes into 'pause' state, your receiver will remain active as your application will keep on running in the background.
Hope this helps.