首页 \ 问答 \ Android BroadcastReceiver和Activity.onPause()(Android BroadcastReceiver and Activity.onPause())

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

更新时间:2023-03-21 21:03

最满意答案

该文件意味着如果您在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 ...
  • 该文件意味着如果您在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 ...

相关文章

更多

最新问答

更多
  • 在javascript中创建类以创建对象并在Java中创建类和对象之间的区别(Difference between creating a class in javascript to create an object and creating an class and object in Java)
  • Facebook API:将身份验证详细信息从Javascript SDK发送到PHP SDK(Facebook API: Send authentication detail from Javascript SDK to PHP SDK)
  • 如何停止队列动画jquery?(How can I stop queue animation jquery?)
  • 使用C#的井字游戏中的人工智能(Artificial Intelligence in Tic-Tac-Toe using C#)
  • 多少流量可以共享虚拟主机(对于Python Django站点)支持?(How Much Traffic Can Shared Web Hosting (for a Python Django site) support?)
  • 带有CIFilters的CAShapeLayer(CAShapeLayer with CIFilters)
  • 如何在Angular 2中读取JSON #text(How to read in Angular 2 the JSON #text)
  • 如何在xml中读取自闭标签的属性?(How to read self closing tag's attribute in xml?)
  • 无法使用http put将图像上传到亚马逊S3(Cannot upload image to amazon s3 using http put)
  • 文件结束无限循环(end of file infinite while-loop)
  • 在cpp的模板(template in cpp)
  • 在构建库时,clang和clang ++有什么区别?(What's the difference between clang and clang++ when building a library?)
  • ng类中的表达式(expression inside ng-class)
  • 在PHP中获取随机布尔值true / false(Get random boolean true/false in PHP)
  • 管道的高效分块用于严格的字节串(Efficient chunking of conduit for strict bytestring)
  • Python ternary_operator(如果其他标志做了其他操作,则执行其他操作)(Python ternary_operator (do-somthing if flag else do-another))
  • Sencha Touch面具发布(Sencha Touch mask ondisclosure)
  • 验证脚本上的通知[重复](Notices on validation script [duplicate])
  • 朋友功能(friend function)
  • 基于角坐标平移和变换平面几何(Translate and transform plane geometry based on corner coordinates)
  • Rails:'如果在本地运行'条件javascript标记包括(Rails: 'if running locally' conditional javascript tag include)
  • 解压文件(Unzipping files)
  • 使用ui-router以角度加载变量状态(loading in variable states with ui-router in angular)
  • 创建Azure云服务需要多长时间?(how long does it take to create an Azure Cloud Service? How to view log information?)
  • 指向整数的指针数组(Array of pointers to integers)
  • Laravel服务提供商没有看到我的包的主要类(Laravel service provider does not see the main class of my package)
  • 这个关于VSS / RSS / PSS / USS的解释是否准确?(Is this explanation about VSS/RSS/PSS/USS accurate?)
  • 在Django-Admin中通过row-id排序显示项目(Ordering the display items by row-id in Django-Admin)
  • 如何使用cythonize启用`--embed`?(How to enable `--embed` with cythonize?)
  • 用于将文本多行设置的Excel脚本(Excel script for ereasing text multiple rows)