简体   繁体   English

Android BroadCastReceiver滞后应用程序

[英]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. 希望这可以帮助。

  • dont register your broadcast receiver in onCreate. 不要在onCreate中注册您的广播接收器。 Registering it in onResume and unregistering in onPause is safe and enough in your case 在您的情况下,在onResume中注册它并在onPause中注销是安全且足够的
  • you must be doing some heavy load processing in your receiver method. 您必须在接收方方法中进行一些重负载处理。 Android offers a 10sec window to perform what ever you want in your receiver otherwise it will declare it as ANR Android提供了一个10秒的窗口来执行接收器中您想要的任何操作,否则它将声明为ANR
  • To avoid lag, load your processing on a new worker thread 为避免延迟,请在新的工作线程上加载处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:该应用程序的Internet BroadcastReceiver - Android: Internet BroadcastReceiver for the application Android:BroadcastReceiver应用程序安装/卸载 - Android: BroadcastReceiver on application install / uninstall Android类和数据,对象,应用程序,BroadcastReceiver的生命周期 - Android lifecycle of classes and data, Object, Application, BroadcastReceiver 从Android中的BroadcastReceiver的onReceive()启动应用程序 - Launching an application from onReceive() of BroadcastReceiver in Android Android-调用BroadcastReceiver时是否实例化Application类? - Android - Is the Application class instantiated when a BroadcastReceiver is called? 正确使用BroadcastReceiver的Android应用程序 - Android application using BroadcastReceiver right way 阻止BroadCastReceiver触发Android中的应用程序更新 - Stop BroadCastReceiver from triggering on Application update in Android ANDROID - 从 BroadcastReceiver 启动其他应用程序 - ANDROID - Launch other application from a BroadcastReceiver Android:如何从BroadcastReceiver恢复应用程序/活动? - Android: How to Resume Application/Activity from BroadcastReceiver? 在Android应用程序中使用BroadCastReceiver获取纬度和经度? - Get the latitude and longtitude using BroadCastReceiver in android Application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM