Android BroadcastReceiver和Activity.onPause()(Android BroadcastReceiver and Activity.onPause())
BroadcastReceiver的文档说 :
如果在Activity.onResume()实现中注册接收器,则应在Activity.onPause()中取消注册。 (暂停时不会收到意图,这将减少不必要的系统开销)。
我做了一个Activity A1的例子,它有一个内部BroadcastReceiver,当Service S1发出sendBroadcast时,它会更新A1接口。 S1花费大约8秒钟完成。
当运行A1并按下主页按钮以使A1调用onPause时,“它仍然从sendBroadcast接收意图”并更新界面,我是否遗漏了某些内容或文档错误?
谢谢The documentation for BroadcastReceiver says:
If registering a receiver in your Activity.onResume() implementation, you should unregister it in Activity.onPause(). (You won't receive intents when paused, and this will cut down on unnecessary system overhead).
I made an example of Activity A1 that has an inner BroadcastReceiver that updates A1 interface when a Service S1 makes a sendBroadcast. S1 spends around 8 seconds to finish.
When running A1 and hitting the home button for making A1 call onPause, "it still receives the intent from sendBroadcast" and updates the interface, am I missing something or the documentation is wrong?
Thanks
最满意答案
该文件意味着如果您在onPause()中取消注册,那么在暂停时您将不会收到广播意图。 如果您没有注册,那么您将继续接收广播意图。 你在onDestroy()中取消注册,但是当按下home键时,只调用onStop()并且不会调用onDestroy()。 因此,您继续接收广播。
The document meant to say that if you unregistered in onPause() then you won't receive broadcast intents when paused. If you do not unregistered then you would continue to receive broadcast intents. You unregistered in onDestroy(), but when the home key is pressed only onStop() is called and onDestroy() will not be called. Thus you continue to receive broadcast.
相关问答
更多-
如果我必须调用unregisterReceiver(),我的虚拟应用程序如何在每次调用时都收到回调? 有两种方法可以设置BroadcastReceiver : registerReceiver() ,仅对由于其他原因而运行的进程有用 清单中的
您的情况听起来像是使用 。 How can my dummy app receive callback at every call if I have to call unregisterReceiver()? There ar ... -
我做错了什么? ToastDisplay的源代码是OK的(我的类似和工作),但它只会收到一些东西,如果它当前在前台(你注册在onResume中的接收器)。 但是如果显示不同的活动(在这种情况下是SendBroadcast活动),它将无法接收任何内容。 相反,您可能想要从第一个活动中启动ToastDisplay? BroadcastReceiver和Activity在不同的用例中是有意义的。 在我的应用程序中,我需要从背景GPS跟踪服务接收通知,并将其显示在活动中(如果活动在前台 )。 没有必要在清单中注册接 ...
-
文档中的这一说明指出了有关使用活动上下文注册BroadcastReceiver时的特殊情况的一些其他信息。 将BroadcastReceiver定义为AndroidManifest.xml文件的一部分时,此情况不适用。 基本上,该注释表明注册到活动上下文的BroadcastReceiver在该活动暂停时不会接收任何广播意图。 因此,应在Activity.onPause()方法中取消注册注册到活动上下文的BroadcastReceiver对象。 在Activity.onPause()方法中取消注册Broadc ...
-
接收器仅接收广播。 只有一个活动属于启动器。 意图和过滤器解释了很多这方面的内容。 要创建一个不可见的活动(在你快速完成时看不到它),用这个主题声明它: @android:style/Theme.Translucent.NoTitleBar 但是如果你有一个不可见的活动(为了避免混淆用户),你最好还添加以下内容: android:noHistory="true" A Receiver receives broadcasts only. Only an activity belongs in the la ...
-
并非每个Phone都发送BOOT_COMPLETED广播。 例如,我认为是HTC,如果它已启动,则发送QUICKBOOT_POWERON Broadcast。 此外,您的logcat可能无法在启动时连接到您的手机。 我建议创建一个Toast进行检查,然后执行Boot-Reciever 。 您可以使用以下代码创建此Toast : Toast toast = Toast.makeText(getApplicationContext(), "Boot-Reciever Started", Toast.LENGT_ ...
-
发送广播很弱。 活动必须直接从服务内部开始。 而不是这个: Intent broadcastIntent = new Intent(); broadcastIntent.setAction(DataBroadcastReceiver.ACTION_RESP); broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT); sendBroadcast(broadcastIntent); 在服务中使用此: Intent intent ...
-
如果您不想使用BroadcastReceiver,请不要使用它。 电池意图是粘性意图所以您可以在不需要BroadcastReceiver的情况下进行检查,我也不认为将接收器置于活动状态是个好主意。 您可以像这样查看活动中的电池内容,而无需编辑清单 IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = context.registerReceiver(null, filter) ...
-
要禁用manifest中定义的receiver ,请使用以下命令: PackageManager pm = context.getPackageManager(); ComponentName component = new ComponentName(context, PhoneCallReceiver.class) pm.setComponentEnabledSetting(component , PackageManager.COMPONENT_ENABLED_STATE_DISABLED , Pac ...
-
Android BroadcastReceiver和Activity.onPause()(Android BroadcastReceiver and Activity.onPause())[2023-03-21]
该文件意味着如果您在onPause()中取消注册,那么在暂停时您将不会收到广播意图。 如果您没有注册,那么您将继续接收广播意图。 你在onDestroy()中取消注册,但是当按下home键时,只调用onStop()并且不会调用onDestroy()。 因此,您继续接收广播。 The document meant to say that if you unregistered in onPause() then you won't receive broadcast intents when paused. ... -
@Override public void onReceive(Context context, Intent intent){ Context appContext = context.getApplicationContext(); 使用appContext您可以启动“正常”活动。 这里描述的是一个例子 public void sendNotificationEmail(String emailBody) { Intent emailIntent = new Intent(In ...