前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Activity中bindService和registerReceiver的清理工作[通俗易懂]

Activity中bindService和registerReceiver的清理工作[通俗易懂]

作者头像
全栈程序员站长
发布2022-10-01 11:48:49
4900
发布2022-10-01 11:48:49
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

在Android开发中,我们经常需要注册BroadcastReceiver和bind service。

接口函数如下:

public Intent registerReceiver(BroadcastReceiver receiver, IntentFilter filter);

public void unregisterReceiver(BroadcastReceiver receiver);

public boolean bindService(Intent service, ServiceConnection conn, int flags);

public void unbindService(ServiceConnection conn);

有的时候,用户只调用了注册函数,或者是bind service,而忘记了unregisterReceiver和unbindService。这时,在Activity退出的时候,会自动这些问题,自动调用unregisterReceiver和unbindService。具体的流程如下:

private void handleDestroyActivity(IBinder token, boolean finishing, int configChanges, boolean getNonConfigInstance) { ActivityClientRecord r = performDestroyActivity(token, finishing, configChanges, getNonConfigInstance); 回调Activity的onDestroy if (r != null) { // Mocked out contexts won’t be participating in the normal // process lifecycle, but if we’re running with a proper // ApplicationContext we need to have it tear down things // cleanly. Context c = r.activity.getBaseContext(); if (c instanceof ContextImpl) { ((ContextImpl) c).scheduleFinalCleanup( r.activity.getClass().getName(), “Activity”); 清理 注册的receiver和bind的service } } if (finishing) { try { ActivityManagerNative.getDefault().activityDestroyed(token); 通知AMS,activity已经destroy } catch (RemoteException ex) { // If the system process has died, it’s game over for everyone. } } mSomeActivitiesChanged = true; }

In ContextImpl.java

final void scheduleFinalCleanup(String who, String what) { mMainThread.scheduleContextCleanup(this, who, what); }

In ActivityThread.java

final void scheduleContextCleanup(ContextImpl context, String who, String what) { ContextCleanupInfo cci = new ContextCleanupInfo(); cci.context = context; cci.who = who; cci.what = what; sendMessage(H.CLEAN_UP_CONTEXT, cci); }

case CLEAN_UP_CONTEXT: ContextCleanupInfo cci = (ContextCleanupInfo)msg.obj; cci.context.performFinalCleanup(cci.who, cci.what); break;

In ContextImpl.java

final void performFinalCleanup(String who, String what) { //Log.i(TAG, “Cleanup up context: ” + this); mPackageInfo.removeContextRegistrations(getOuterContext(), who, what); }

In LoadedApk.java

public void removeContextRegistrations(Context context, String who, String what) { final boolean reportRegistrationLeaks = StrictMode.vmRegistrationLeaksEnabled(); ArrayMap<BroadcastReceiver, LoadedApk.ReceiverDispatcher> rmap = mReceivers.remove(context); if (rmap != null) { for (int i=0; i<rmap.size(); i++) { 清理所有已经注册的receiver LoadedApk.ReceiverDispatcher rd = rmap.valueAt(i); IntentReceiverLeaked leak = new IntentReceiverLeaked( what + ” ” + who + ” has leaked IntentReceiver “ + rd.getIntentReceiver() + ” that was ” + “originally registered here. Are you missing a ” + “call to unregisterReceiver()?”); leak.setStackTrace(rd.getLocation().getStackTrace()); Slog.e(ActivityThread.TAG, leak.getMessage(), leak); if (reportRegistrationLeaks) { StrictMode.onIntentReceiverLeaked(leak); } try { ActivityManagerNative.getDefault().unregisterReceiver( rd.getIIntentReceiver()); } catch (RemoteException e) { // system crashed, nothing we can do } } } mUnregisteredReceivers.remove(context); //Slog.i(TAG, “Receiver registrations: ” + mReceivers);

ArrayMap<ServiceConnection, LoadedApk.ServiceDispatcher> smap = mServices.remove(context); if (smap != null) { for (int i=0; i<smap.size(); i++) { 解绑定已经bind的service LoadedApk.ServiceDispatcher sd = smap.valueAt(i); ServiceConnectionLeaked leak = new ServiceConnectionLeaked( what + ” ” + who + ” has leaked ServiceConnection “ + sd.getServiceConnection() + ” that was originally bound here”); leak.setStackTrace(sd.getLocation().getStackTrace()); Slog.e(ActivityThread.TAG, leak.getMessage(), leak); if (reportRegistrationLeaks) { StrictMode.onServiceConnectionLeaked(leak); } try { ActivityManagerNative.getDefault().unbindService( sd.getIServiceConnection()); } catch (RemoteException e) { // system crashed, nothing we can do } sd.doForget(); } } mUnboundServices.remove(context); //Slog.i(TAG, “Service registrations: ” + mServices); }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/195003.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年9月11日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档