简体   繁体   English

系统何时将开始考虑动态注册的BroadcastReceiver?

[英]When will dynamically registered BroadcastReceiver start being considered by system?

My question is about: "when will my postbox be noticed by the postman", not "when will I be able to read any letters"? 我的问题是关于:“邮递员何时会看到我的邮箱”,而不是“什么时候可以阅读任何信件”?

I have an Activity which allows the user to edit a list. 我有一个Activity ,允许用户编辑列表。 As soon as the user commits the edit, an IntentService is started which in turn calls a database helper to change the database entries as required. 用户提交编辑后,将IntentService启动IntentService ,该服务又调用数据库助手以根据需要更改数据库条目。

As soon as the database is updated successfully (nothing crashed until then), the database helper writes "ok" to SharedPreferences and returns to the IntentService . 一旦数据库成功更新(此后才崩溃),数据库助手将“ ok”写入SharedPreferences并返回到IntentService The IntentService sends a message to a BroadcastReceiver which now causes the list to be reloaded from db with the current data. IntentService将消息发送到BroadcastReceiver ,这现在使该列表从db中使用当前数据重新加载。

The BroadcastReceiver is an inner class of the Activity and it is registered dynamically in onCreate() : BroadcastReceiverActivity的内部类,并且在onCreate()动态注册:

    mReceiver = new MyMessageReceiver();
    IntentFilter iFilter = new IntentFilter();
    iFilter.addAction("com.example.mycoolapp.LOCALINTENT");
    LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, iFilter);

If the user stayed in my Activity until the list could be reloaded, there would be no problem at all. 如果用户一直停留在“我的Activity直到可以重新加载列表,那么就不会有任何问题。 But imagine the user moving around and somehow causing an orientation change... that's why I have the "ok" in SharedPeferences : 但是,想象一下用户到处走动并以某种方式导致方向改变...这就是为什么我在SharedPeferences “确定”的SharedPeferences

In onCreate() , after registering the receiver, I check whether there is an action pending: onCreate() ,注册接收者之后,我检查是否有待处理的操作:

If the database was updated successfully, I can load the data and just set a flag for my receiver to ignore the next message. 如果数据库更新成功,我可以加载数据,并为接收者设置一个标志以忽略下一条消息。 Which will come, as the IntentService was configured with setIntentRedelivery(true) . 即将出现,因为IntentService是使用setIntentRedelivery(true)配置的。

If I find that the database is not yet updated, I decide to wait for the broadcast from the IntentService . 如果发现数据库尚未更新,则决定等待IntentService的广播。

The one thing I'm extremely uncertain of is the following: 我非常不确定的一件事是:

From which moment on will the system consider my receiver to be "up and running"? 从哪一刻起,系统将认为我的接收器“已启动并正在运行”? I know that onReceive() can only be called after onCreate() is finished. 我知道onReceive()只能在onCreate()完成后才能调用。

Is it possible that while onCreate() is being executed, the IntentService fires the local Broadcast ("too soon") and my receiver will never get it? 是否有可能在执行onCreate() ,IntentService触发本地广播(“太早”),而我的接收器将永远无法获得它?

Then that list would never be refreshed until the user left the Activity (which is quite likely) and returned once more (then not so likely). 然后,直到用户离开“ Activity (很有可能)并再次返回(然后不太可能)之前,该列表才不会刷新。

The Broadcast Receiver is not registered in the Activity but in the main application thread. 广播接收器未在活动中注册,而是在主应用程序线程中注册。 The registerReceiver method is defined in the Context class. registerReceiver方法在Context类中定义。

That means that your Broadcast receiver will remain registered until you un-register it or your application is destroyed (ie. not having the receiver in a Service ) 这意味着您的广播接收器将一直保持registered状态,直到您取消注册它或您的应用程序被破坏(即Service没有接收器)为止

Check the documentation 检查文件

From which moment on will the system consider my receiver to be "up and running"? 从哪一刻起,系统将认为我的接收器“已启动并正在运行”? I know that onReceive() can only be called after onCreate() is finished. 我知道onReceive()只能在onCreate()完成后才能调用。

From the moment registerReceiver() returns 从registerReceiver()返回的那一刻起

Is it possible that while onCreate() is being executed, the IntentService fires the local Broadcast ("too soon") and my receiver will never get it? 是否有可能在执行onCreate()时,IntentService触发本地广播(“太早”),而我的接收器将永远无法获得它?

You can avoid this by registering the receiver in the onCreate method of a class that extends Application . 您可以通过在扩展 Application的类的onCreate方法中注册接收方来避免这种情况。 That's how you can make sure that the broadcast receiver is the first thing done. 这样可以确保广播接收器是第一件事。

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

相关问题 BroadcastReceiver onReceive()在动态注册时不调用 - BroadcastReceiver onReceive() not Called when registered dynamically 使用LocalBroadcastManager注册BroadcastReceiver时,不会调用onReceive - onReceive not being called when BroadcastReceiver is registered using LocalBroadcastManager 动态注册的 BroadcastReceiver 实例没有触发? - Dynamically registered BroadcastReceiver instance not firing? 动态注册BroadcastReceiver-未注册接收器 - Dynamically registering BroadcastReceiver - Receiver not registered 注册时触发BroadcastReceiver onReceive - BroadcastReceiver onReceive triggered when registered 在清单中注册时需要注销Android Broadcastreceiver吗? - Android Broadcastreceiver unregister needed when registered in manifest? 在动态注册广播接收器的onReceive方法时,它总是被调用吗? - Is a BroadcastReceiver's onReceive method always called the moment it gets registered dynamically? Android BroadcastReceiver生命周期:动态注册接收器的文档错误? - Android BroadcastReceiver Lifecycle: documentation wrong for dynamically registered receiver? 广播接收器被视为服务吗? - Is broadcastreceiver considered a service? 当仅使用BroadcastReceiver时,FLAG_RECEIVER_REGISTERED_ONLY是否有解决方法? - Is there a workaround for FLAG_RECEIVER_REGISTERED_ONLY, when using only a BroadcastReceiver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM