泄露的意图和报警管理器

发布于 2024-09-16 03:31:31 字数 4186 浏览 6 评论 0原文

我正在尝试创建身份验证系统,如果设备关闭 (SCREEN_OFF) 时间超过 INTERVAL,该系统会弹出登录窗口。

我已经注册了一个 BroadcastReceiver 来监听可启动活动的 onCreate() 中的 SCREEN_ON/OFF 事件:

/* Listen to Screen On & Off events */
  IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
  filter.addAction(Intent.ACTION_SCREEN_OFF);
  BroadcastReceiver screenOnOff = new ScreenOnOff();
  registerReceiver(screenOnOff, filter);

ScreenOnOff -- 是一个 BroadcastReceiver,其功能包括:

@Override
 public void onReceive(Context context, Intent intent) {


mAuthenticationIntent = PendingIntent.getActivity(context, 0,
    new Intent(context, AuthenticationActivity.class), 0);

String action = intent.getAction();

if (action.equals(Intent.ACTION_SCREEN_OFF)) {
   AlarmManager mAlarmManager = (AlarmManager) context
     .getSystemService(context.ALARM_SERVICE);
   mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, System
     .currentTimeMillis()
     + interval, mAuthenticationIntent);

   Log.d(LOG_TAG, "Alarm Set!");
  }

当警报响起时,程序崩溃:

08-27 19:18:02.207: ERROR/ActivityThread(3165): Activity com.artoo.ArtooSlateActivity has leaked IntentReceiver com.artoo.authentication.ScreenOnOff@4495bca0 that was originally registered here. Are you missing a call to unregisterReceiver()?
08-27 19:18:02.207: ERROR/ActivityThread(3165): android.app.IntentReceiverLeaked: Activity com.artoo.ArtooSlateActivity has leaked IntentReceiver com.artoo.authentication.ScreenOnOff@4495bca0 that was originally registered here. Are you missing a call to unregisterReceiver()?
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ActivityThread$PackageInfo$ReceiverDispatcher.<init>(ActivityThread.java:939)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ActivityThread$PackageInfo.getReceiverDispatcher(ActivityThread.java:734)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:791)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:778)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:772)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at com.artoo.ArtooSlateActivity.onCreate(ArtooSlateActivity.java:50)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3815)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ActivityThread.access$2400(ActivityThread.java:125)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2037)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.os.Looper.loop(Looper.java:123)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at java.lang.reflect.Method.invokeNative(Native Method)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at java.lang.reflect.Method.invoke(Method.java:521)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at dalvik.system.NativeStart.main(Native Method)

任何帮助将不胜感激。

谢谢,萨米尔

I am trying to create authentication system which pops up the login window if the device has been off (SCREEN_OFF) for more than INTERVAL.

I have registered a BroadcastReceiver to listen to SCREEN_ON/OFF events in the onCreate() of the launchable activity:

/* Listen to Screen On & Off events */
  IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
  filter.addAction(Intent.ACTION_SCREEN_OFF);
  BroadcastReceiver screenOnOff = new ScreenOnOff();
  registerReceiver(screenOnOff, filter);

ScreenOnOff -- is a BroadcastReceiver which among other things does:

@Override
 public void onReceive(Context context, Intent intent) {


mAuthenticationIntent = PendingIntent.getActivity(context, 0,
    new Intent(context, AuthenticationActivity.class), 0);

String action = intent.getAction();

if (action.equals(Intent.ACTION_SCREEN_OFF)) {
   AlarmManager mAlarmManager = (AlarmManager) context
     .getSystemService(context.ALARM_SERVICE);
   mAlarmManager.set(AlarmManager.ELAPSED_REALTIME, System
     .currentTimeMillis()
     + interval, mAuthenticationIntent);

   Log.d(LOG_TAG, "Alarm Set!");
  }

When the alarm goes off, the program crashes:

08-27 19:18:02.207: ERROR/ActivityThread(3165): Activity com.artoo.ArtooSlateActivity has leaked IntentReceiver com.artoo.authentication.ScreenOnOff@4495bca0 that was originally registered here. Are you missing a call to unregisterReceiver()?
08-27 19:18:02.207: ERROR/ActivityThread(3165): android.app.IntentReceiverLeaked: Activity com.artoo.ArtooSlateActivity has leaked IntentReceiver com.artoo.authentication.ScreenOnOff@4495bca0 that was originally registered here. Are you missing a call to unregisterReceiver()?
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ActivityThread$PackageInfo$ReceiverDispatcher.<init>(ActivityThread.java:939)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ActivityThread$PackageInfo.getReceiverDispatcher(ActivityThread.java:734)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:791)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:778)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ContextImpl.registerReceiver(ContextImpl.java:772)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:318)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at com.artoo.ArtooSlateActivity.onCreate(ArtooSlateActivity.java:50)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3815)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ActivityThread.access$2400(ActivityThread.java:125)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2037)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.os.Looper.loop(Looper.java:123)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at android.app.ActivityThread.main(ActivityThread.java:4627)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at java.lang.reflect.Method.invokeNative(Native Method)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at java.lang.reflect.Method.invoke(Method.java:521)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-27 19:18:02.207: ERROR/ActivityThread(3165):     at dalvik.system.NativeStart.main(Native Method)

Any help will be most appreciated.

Thanks, Sameer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光无声 2024-09-23 03:31:31

由于您动态注册接收器,系统知道您正在泄漏内存,因为您从未对其调用 unregister 。显然你不想取消注册,因为你需要监听 SCREEN_ON/OFF 事件。

解决方案是在 Manifest.xml 中注册接收器

用于接收由 sendBroadcast() 发送的意图的代码的基类。您可以使用 Context.registerReceiver() 动态注册此类的实例,也可以通过 AndroidManifest.xml 中的标记静态发布实现。注意:如果在 Activity.onResume() 实现中注册接收器,则应在 Activity.onPause() 中取消注册它。 (暂停时您不会收到意图,这将减少不必要的系统开销)。不要在 Activity.onSaveInstanceState() 中取消注册,因为如果用户在历史堆栈中移回,则不会调用此方法

Since you are registering the receiver dynamically the system knows you are leaking memory because you never call unregister on it. Obviously you don't want to unregister because you need to listen for SCREEN_ON/OFF events.

The Solution is to register the receiver in your Manifest.xml

Base class for code that will receive intents sent by sendBroadcast(). You can either dynamically register an instance of this class with Context.registerReceiver() or statically publish an implementation through the tag in your AndroidManifest.xml. Note: 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). Do not unregister in Activity.onSaveInstanceState(), because this won't be called if the user moves back in the history stack

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文