[英]Android BroadCastReceiver lagging application
I have an issue with BroadcastReceiver
which I'm using in my activities. 我在活动中使用的
BroadcastReceiver
有问题。 I'm actually doing this : 我实际上是在这样做:
In onCreate()
: 在
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 : 在
onResume()
和onPause()
我正在这样做:
@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 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. 当我在6个需要添加此广播接收器的活动中执行此操作时,我的应用程序开始滞后并且变得比以前慢。
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! 提前致谢!
Instead of registering your receiver with Activity's context, register it with your application's context in your 1st activity as below: 与其在Activity的上下文中注册接收者,不如在您的第一个活动中在应用程序的上下文中注册它,如下所示:
getApplication().registerReceiver(receiver, intentFilter);
getApplication()。registerReceiver(receiver,intentFilter);
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. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.