Activity泄露了IntentReceiver - LollipopBrowserAccessibilityManager [英] Activity has leaked IntentReceiver - LollipopBrowserAccessibilityManager
问题描述
我希望在这里找到一些帮助,因为我不熟悉Android中的BroadcastReceivers。这段代码打开WebView会将您重定向到登录页面,并在检测到URL更改后接收登录令牌。之后,活动将关闭。
I hope to find some help here because I'm not familiar with BroadcastReceivers in Android. This piece of code opens a WebView redirect you to a login page and receives the login token once a URL change is detected. After that, the Activity is closed.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mLoginWebView = (WebView) findViewById(R.id.webview_login);
redirectUrl = getString(R.string.app_redirect_url);
//RECEIVE PLATFORM ID
Bundle bundle = getIntent().getExtras();
if(bundle != null){
platform = bundle.getInt(ConstantsHelper.LOGIN_EXTRA_TOKEN);
}
mLoginWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d(TAG, "URL change to to " + url + " was detected");
if (url.contains(redirectUrl) || url.contains("passport.twitch.tv")) {
Log.d(TAG, "Login with platform " + platform);
switch (platform){
//GET INSTAGRAM AUTH TOKEN
case ConstantsHelper.ID_INSTAGRAM:{
String accessToken = url.split("=")[1];
SharedPreferenceHelper.putString(ConstantsHelper.PREF_INST_ACCESS_TOKEN, accessToken);
NetworkManager.getInstance().catchTokens();
}
//GET TWITCH AUTH TOKEN
case ConstantsHelper.ID_TWITCH:{
String accessToken = url.substring(url.indexOf("=") + 1, url.indexOf("&"));
SharedPreferenceHelper.putString(ConstantsHelper.PREF_TWITCH_ACCESS_TOKEN, accessToken);
NetworkManager.getInstance().catchTokens();
}
}
finish(); //Activity is closed
return true;
}
return false;
}
});
switch (platform){
case 1: mLoginWebView.loadUrl(NetworkManager.getInstance().getInstagramAuthUrl(getApplicationContext()));
case 4: mLoginWebView.loadUrl(NetworkManager.getInstance().getTwitchAuthUrl(getApplicationContext()));
}
}
没有任何反应, LogCat显示此错误:
Nothing happens and LogCat is displaying this error:
Activity com.maximutan.socialmedia_feed_merger.activities.LoginActivity has leaked IntentReceiver org.chromium.content.browser.accessibility.LollipopBrowserAccessibilityManager$1@25014a that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Activity com.maximutan.socialmedia_feed_merger.activities.LoginActivity has leaked IntentReceiver org.chromium.content.browser.accessibility.LollipopBrowserAccessibilityManager$1@25014a that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:962)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:763)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1179)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1159)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1153)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:554)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:554)
at org.chromium.content.browser.accessibility.LollipopBrowserAccessibilityManager.<init>(LollipopBrowserAccessibilityManager.java:3)
at org.chromium.content.browser.accessibility.BrowserAccessibilityManager.create(BrowserAccessibilityManager.java:2)
at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
at org.chromium.base.SystemMessageHandler.handleMessage(
SystemMessageHandler.java:7)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5527)
,at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(
ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
我的问题是我不知道我必须取消注册哪个BroadcastReceiver,因为我没有创建和初始化。
谢谢为您提供帮助
推荐答案
调用 destroy()
时会出现此问题当WebView仍然附加到其父视图时,在WebView上。
This issue occurs when destroy()
is called on the WebView when the WebView is still attached to its parent view.
在调用 destroy()
。
请注意即使你没有调用 destroy()
manua lly(就像我的情况一样)这次崩溃仍然可能发生。我发现关键是你必须明确销毁一个WebView(至少在片段中),因为Android不会为你处理这个问题,在这之前你必须从它的父视图中删除它。
Note that even if you are not calling destroy()
manually (like was the case for me) this crash can still occur. I found that the key is that you have to explicitly destroy a WebView (at least in a fragment), as Android doesn’t handle this for you, and prior to doing that, you have to remove it from its parent view.
例如,如果你有一个WebView片段:
For example, if you have a WebView fragment:
@Override
public void onDestroyView() {
super.onDestroyView();
// destroy the WebView completely
if (mWebView != null) {
// the WebView must be removed from the view hierarchy before calling destroy
// to prevent a memory leak
// See https://developer.android.com/reference/android/webkit/WebView.html#destroy%28%29
((ViewGroup) mWebView.getParent()).removeView(mWebView);
mWebView.removeAllViews();
mWebView.destroy();
mWebView = null;
}
}
Billy Brawner的全部功劳: https://brawner.tech/2017/12/03/webview-memory-leak/
Full credit to Billy Brawner: https://brawner.tech/2017/12/03/webview-memory-leak/
这篇关于Activity泄露了IntentReceiver - LollipopBrowserAccessibilityManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!