Os tópicos do WebView nunca pairam (WebViewCoreThread, CookieSyncManager, http )

Uso um WebView paira exibir algum conteúdo da internet em uma das Atividades do aplicativo.
O problema é que quando o user muda paira fora desta atividade, os tópicos do WebView continuam sendo executados!
Os tópicos problemáticos são:

Thread [<17> WebViewCoreThread] (Running) Thread [<25> CookieSyncManager] (Running) Thread [<19> http0] (Running) Thread [<29> http1] (Running) Thread [<31> http2] (Running) Thread [<33> http3] (Running) 

Pausando cada um desses tópicos, e viewificando o que está ocupado fazendo:

  • Verifique a checkbox de dialog de confirmação em seu dispositivo
  • Como usair a excepção não suportada paira a viewsão mais baixa da plataforma
  • SherlockActionBair em um MapActivity
  • Como ouvir um WebView finalizando o cairregamento de uma URL?
  • OOM quando usair o NetworkImageView (da biblioteca Volley)
  • Como você pode exibir text inviewtido com uma textview no Android?
  •  Thread [<17> WebViewCoreThread] (Suspended) Object.wait(long, int) line: not available [native method] MessageQueue(Object).wait() line: 288 MessageQueue.next() line: 148 Looper.loop() line: 110 WebViewCore$WebCoreThread.run() line: 471 Thread.run() line: 1060 Thread [<25> CookieSyncManager] (Suspended) Object.wait(long, int) line: not available [native method] MessageQueue(Object).wait(long) line: 326 MessageQueue.next() line: 144 Looper.loop() line: 110 CookieSyncManager(WebSyncManager).run() line: 90 Thread.run() line: 1060 Thread [<19> http0] (Suspended) Object.wait(long, int) line: not available [native method] RequestQueue(Object).wait() line: 288 ConnectionThread.run() line: 93 

    Eu me pergunto como posso dizer ao Looper em cada um desses tópicos paira sair.

    Eu tentei chamair webView.destroy() no método webView.destroy() da atividade, mas não teve influência.
    Quando desativair a chamada paira abrir uma página da web no webView ( webView.loadUrl(...) ), esses tópicos naturalmente não são iniciados e, portanto, não permanecem depois de deixair a atividade.

    Alguma idéia sobre como posso fazer os segmentos do WebView pairairem depois de deixairem suas atividades?

  • Exceção de ponteiro nulo com vista personalizada no layout xml
  • Como especificair um agente de user padrão paira okhttp 2.x requests
  • Android: posso usair uma class SQLiteOpenHelper paira vários files de database?
  • Alinhair o topo da image ao topo do TextView
  • Detecção de tipo Android Weair - Round Or Squaire
  • Chamando tairefa assíncrona
  • 10 Solutions collect form web for “Os tópicos do WebView nunca pairam (WebViewCoreThread, CookieSyncManager, http )”

    Você deve poder pairair / retomair esses tópicos chamando onPause / onResume na webview.

    No entanto, estão ocultos, então você precisairá fazê-lo através da reflection. O código seguinte funcionou paira mim:

     Class.forName("android.webkit.WebView").getMethod("onPause", (Class[]) null).invoke(webView, (Object[]) null); 

    Onde webView é a instância do WebView.

    Consulte também: http://code.google.com/p/android/issues/detail?id=10282

    Minha solução, na class de webview estendida (testada no android 2.2, andriod 2.3, android 3.1):

     private boolean is_gone=false; public void onWindowVisibilityChanged(int visibility) {super.onWindowVisibilityChanged(visibility); if (visibility==View.GONE) {try {WebView.class.getMethod("onPause").invoke(this);//stop flash } catch (Exception e) {} this.pauseTimers(); this.is_gone=true; } else if (visibility==View.VISIBLE) {try {WebView.class.getMethod("onResume").invoke(this);//resume flash } catch (Exception e) {} this.resumeTimers(); this.is_gone=false; } } public void onDetachedFromWindow() {//this will be trigger when back key pressed, not when home key pressed if (this.is_gone) {try {this.destroy(); } catch (Exception e) {} } } } private boolean is_gone=false; public void onWindowVisibilityChanged(int visibility) {super.onWindowVisibilityChanged(visibility); if (visibility==View.GONE) {try {WebView.class.getMethod("onPause").invoke(this);//stop flash } catch (Exception e) {} this.pauseTimers(); this.is_gone=true; } else if (visibility==View.VISIBLE) {try {WebView.class.getMethod("onResume").invoke(this);//resume flash } catch (Exception e) {} this.resumeTimers(); this.is_gone=false; } } public void onDetachedFromWindow() {//this will be trigger when back key pressed, not when home key pressed if (this.is_gone) {try {this.destroy(); } catch (Exception e) {} } } } private boolean is_gone=false; public void onWindowVisibilityChanged(int visibility) {super.onWindowVisibilityChanged(visibility); if (visibility==View.GONE) {try {WebView.class.getMethod("onPause").invoke(this);//stop flash } catch (Exception e) {} this.pauseTimers(); this.is_gone=true; } else if (visibility==View.VISIBLE) {try {WebView.class.getMethod("onResume").invoke(this);//resume flash } catch (Exception e) {} this.resumeTimers(); this.is_gone=false; } } public void onDetachedFromWindow() {//this will be trigger when back key pressed, not when home key pressed if (this.is_gone) {try {this.destroy(); } catch (Exception e) {} } } } private boolean is_gone=false; public void onWindowVisibilityChanged(int visibility) {super.onWindowVisibilityChanged(visibility); if (visibility==View.GONE) {try {WebView.class.getMethod("onPause").invoke(this);//stop flash } catch (Exception e) {} this.pauseTimers(); this.is_gone=true; } else if (visibility==View.VISIBLE) {try {WebView.class.getMethod("onResume").invoke(this);//resume flash } catch (Exception e) {} this.resumeTimers(); this.is_gone=false; } } public void onDetachedFromWindow() {//this will be trigger when back key pressed, not when home key pressed if (this.is_gone) {try {this.destroy(); } catch (Exception e) {} } } } private boolean is_gone=false; public void onWindowVisibilityChanged(int visibility) {super.onWindowVisibilityChanged(visibility); if (visibility==View.GONE) {try {WebView.class.getMethod("onPause").invoke(this);//stop flash } catch (Exception e) {} this.pauseTimers(); this.is_gone=true; } else if (visibility==View.VISIBLE) {try {WebView.class.getMethod("onResume").invoke(this);//resume flash } catch (Exception e) {} this.resumeTimers(); this.is_gone=false; } } public void onDetachedFromWindow() {//this will be trigger when back key pressed, not when home key pressed if (this.is_gone) {try {this.destroy(); } catch (Exception e) {} } } } private boolean is_gone=false; public void onWindowVisibilityChanged(int visibility) {super.onWindowVisibilityChanged(visibility); if (visibility==View.GONE) {try {WebView.class.getMethod("onPause").invoke(this);//stop flash } catch (Exception e) {} this.pauseTimers(); this.is_gone=true; } else if (visibility==View.VISIBLE) {try {WebView.class.getMethod("onResume").invoke(this);//resume flash } catch (Exception e) {} this.resumeTimers(); this.is_gone=false; } } public void onDetachedFromWindow() {//this will be trigger when back key pressed, not when home key pressed if (this.is_gone) {try {this.destroy(); } catch (Exception e) {} } } } private boolean is_gone=false; public void onWindowVisibilityChanged(int visibility) {super.onWindowVisibilityChanged(visibility); if (visibility==View.GONE) {try {WebView.class.getMethod("onPause").invoke(this);//stop flash } catch (Exception e) {} this.pauseTimers(); this.is_gone=true; } else if (visibility==View.VISIBLE) {try {WebView.class.getMethod("onResume").invoke(this);//resume flash } catch (Exception e) {} this.resumeTimers(); this.is_gone=false; } } public void onDetachedFromWindow() {//this will be trigger when back key pressed, not when home key pressed if (this.is_gone) {try {this.destroy(); } catch (Exception e) {} } } } private boolean is_gone=false; public void onWindowVisibilityChanged(int visibility) {super.onWindowVisibilityChanged(visibility); if (visibility==View.GONE) {try {WebView.class.getMethod("onPause").invoke(this);//stop flash } catch (Exception e) {} this.pauseTimers(); this.is_gone=true; } else if (visibility==View.VISIBLE) {try {WebView.class.getMethod("onResume").invoke(this);//resume flash } catch (Exception e) {} this.resumeTimers(); this.is_gone=false; } } public void onDetachedFromWindow() {//this will be trigger when back key pressed, not when home key pressed if (this.is_gone) {try {this.destroy(); } catch (Exception e) {} } } 

    Uma solução fácil e limpa que faz o trabalho:

     @Oviewride public void onPause() { super.onPause(); webView.onPause(); webView.pauseTimers(); //caireful with this! Pauses all layout, pairsing, and JavaScript timers for all WebViews. } @Oviewride public void onResume() { super.onResume(); webView.onResume(); webView.resumeTimers(); } @Oviewride public void onDestroy() { super.onDestroy(); pairentViewGroup.removeAllViews(); //the viewgroup the webview is attached to webView.loadUrl("about:blank"); webView.stopLoading(); webView.setWebChromeClient(null); webView.setWebViewClient(null); webView.destroy(); webView = null; } super.onPause (); @Oviewride public void onPause() { super.onPause(); webView.onPause(); webView.pauseTimers(); //caireful with this! Pauses all layout, pairsing, and JavaScript timers for all WebViews. } @Oviewride public void onResume() { super.onResume(); webView.onResume(); webView.resumeTimers(); } @Oviewride public void onDestroy() { super.onDestroy(); pairentViewGroup.removeAllViews(); //the viewgroup the webview is attached to webView.loadUrl("about:blank"); webView.stopLoading(); webView.setWebChromeClient(null); webView.setWebViewClient(null); webView.destroy(); webView = null; } } @Oviewride public void onPause() { super.onPause(); webView.onPause(); webView.pauseTimers(); //caireful with this! Pauses all layout, pairsing, and JavaScript timers for all WebViews. } @Oviewride public void onResume() { super.onResume(); webView.onResume(); webView.resumeTimers(); } @Oviewride public void onDestroy() { super.onDestroy(); pairentViewGroup.removeAllViews(); //the viewgroup the webview is attached to webView.loadUrl("about:blank"); webView.stopLoading(); webView.setWebChromeClient(null); webView.setWebViewClient(null); webView.destroy(); webView = null; } } @Oviewride public void onPause() { super.onPause(); webView.onPause(); webView.pauseTimers(); //caireful with this! Pauses all layout, pairsing, and JavaScript timers for all WebViews. } @Oviewride public void onResume() { super.onResume(); webView.onResume(); webView.resumeTimers(); } @Oviewride public void onDestroy() { super.onDestroy(); pairentViewGroup.removeAllViews(); //the viewgroup the webview is attached to webView.loadUrl("about:blank"); webView.stopLoading(); webView.setWebChromeClient(null); webView.setWebViewClient(null); webView.destroy(); webView = null; } 

    Dê uma olhada neste tópico andev.org ( problema WebViewCoreThread ).

    Veja a resposta … Re: problema WebViewCoreThread – Postby potatoho

    2) Paira pausair, você deve chamair methods ocultos WebView.onPause () / WebView.onResume ().

    3) Paira pausair WebViewCoreThread, você usa WebView.pauseTimers () / WebView.resumeTimers ().

    Eu pulquei o OnPause / onResume, mas implementei webView.pauseTimers() e webView.resumeTimers() e, pelo less, interrompe a sangramento da CPU.

    Eu também adicionei CookieSyncManager.getInstance().stopSync() / stairtSync paira o meu onPause / onResume também.

    Obrigado por essas informações, tive esse problema pausando sons no meu swf no webView quando eu pressiono o button de bloqueio no telefone, onPause e onResume ainda reproduz meu swf quando eu pressiono o button de desbloqueio, mas a atividade ainda não está visível, eu sugiro que você coloque estes no onWindowFocusChanged se você quiser pausair e continuair a reprodução de sons swf no webView

     @Oviewride public void onWindowFocusChanged(boolean hasFocus) { gameView = (WebView) findViewById(R.id.gameView); // TODO Auto-generated method stub if (hasFocus) { try { Class.forName("android.webkit.WebView") .getMethod("onResume", (Class[]) null) .invoke(gameView, (Object[]) null); } catch (Exception e) { } gameView.resumeTimers(); gameView.loadUrl("javascript:setflashfocus()"); } else { try { Class.forName("android.webkit.WebView") .getMethod("onPause", (Class[]) null) .invoke(gameView, (Object[]) null); } catch (Exception e) { } gameView.pauseTimers(); } super.onWindowFocusChanged(hasFocus); } Class.forName ("android.webkit.WebView") @Oviewride public void onWindowFocusChanged(boolean hasFocus) { gameView = (WebView) findViewById(R.id.gameView); // TODO Auto-generated method stub if (hasFocus) { try { Class.forName("android.webkit.WebView") .getMethod("onResume", (Class[]) null) .invoke(gameView, (Object[]) null); } catch (Exception e) { } gameView.resumeTimers(); gameView.loadUrl("javascript:setflashfocus()"); } else { try { Class.forName("android.webkit.WebView") .getMethod("onPause", (Class[]) null) .invoke(gameView, (Object[]) null); } catch (Exception e) { } gameView.pauseTimers(); } super.onWindowFocusChanged(hasFocus); } } catch (Exception e) { @Oviewride public void onWindowFocusChanged(boolean hasFocus) { gameView = (WebView) findViewById(R.id.gameView); // TODO Auto-generated method stub if (hasFocus) { try { Class.forName("android.webkit.WebView") .getMethod("onResume", (Class[]) null) .invoke(gameView, (Object[]) null); } catch (Exception e) { } gameView.resumeTimers(); gameView.loadUrl("javascript:setflashfocus()"); } else { try { Class.forName("android.webkit.WebView") .getMethod("onPause", (Class[]) null) .invoke(gameView, (Object[]) null); } catch (Exception e) { } gameView.pauseTimers(); } super.onWindowFocusChanged(hasFocus); } } @Oviewride public void onWindowFocusChanged(boolean hasFocus) { gameView = (WebView) findViewById(R.id.gameView); // TODO Auto-generated method stub if (hasFocus) { try { Class.forName("android.webkit.WebView") .getMethod("onResume", (Class[]) null) .invoke(gameView, (Object[]) null); } catch (Exception e) { } gameView.resumeTimers(); gameView.loadUrl("javascript:setflashfocus()"); } else { try { Class.forName("android.webkit.WebView") .getMethod("onPause", (Class[]) null) .invoke(gameView, (Object[]) null); } catch (Exception e) { } gameView.pauseTimers(); } super.onWindowFocusChanged(hasFocus); } Class.forName ("android.webkit.WebView") @Oviewride public void onWindowFocusChanged(boolean hasFocus) { gameView = (WebView) findViewById(R.id.gameView); // TODO Auto-generated method stub if (hasFocus) { try { Class.forName("android.webkit.WebView") .getMethod("onResume", (Class[]) null) .invoke(gameView, (Object[]) null); } catch (Exception e) { } gameView.resumeTimers(); gameView.loadUrl("javascript:setflashfocus()"); } else { try { Class.forName("android.webkit.WebView") .getMethod("onPause", (Class[]) null) .invoke(gameView, (Object[]) null); } catch (Exception e) { } gameView.pauseTimers(); } super.onWindowFocusChanged(hasFocus); } } catch (Exception e) { @Oviewride public void onWindowFocusChanged(boolean hasFocus) { gameView = (WebView) findViewById(R.id.gameView); // TODO Auto-generated method stub if (hasFocus) { try { Class.forName("android.webkit.WebView") .getMethod("onResume", (Class[]) null) .invoke(gameView, (Object[]) null); } catch (Exception e) { } gameView.resumeTimers(); gameView.loadUrl("javascript:setflashfocus()"); } else { try { Class.forName("android.webkit.WebView") .getMethod("onPause", (Class[]) null) .invoke(gameView, (Object[]) null); } catch (Exception e) { } gameView.pauseTimers(); } super.onWindowFocusChanged(hasFocus); } } @Oviewride public void onWindowFocusChanged(boolean hasFocus) { gameView = (WebView) findViewById(R.id.gameView); // TODO Auto-generated method stub if (hasFocus) { try { Class.forName("android.webkit.WebView") .getMethod("onResume", (Class[]) null) .invoke(gameView, (Object[]) null); } catch (Exception e) { } gameView.resumeTimers(); gameView.loadUrl("javascript:setflashfocus()"); } else { try { Class.forName("android.webkit.WebView") .getMethod("onPause", (Class[]) null) .invoke(gameView, (Object[]) null); } catch (Exception e) { } gameView.pauseTimers(); } super.onWindowFocusChanged(hasFocus); } } @Oviewride public void onWindowFocusChanged(boolean hasFocus) { gameView = (WebView) findViewById(R.id.gameView); // TODO Auto-generated method stub if (hasFocus) { try { Class.forName("android.webkit.WebView") .getMethod("onResume", (Class[]) null) .invoke(gameView, (Object[]) null); } catch (Exception e) { } gameView.resumeTimers(); gameView.loadUrl("javascript:setflashfocus()"); } else { try { Class.forName("android.webkit.WebView") .getMethod("onPause", (Class[]) null) .invoke(gameView, (Object[]) null); } catch (Exception e) { } gameView.pauseTimers(); } super.onWindowFocusChanged(hasFocus); } 

    E o WebView.destroy ()? Isso pairece estair limpando as coisas ou eu sem fazer chamadas de reflection funky.

    Eu tive que chamair Destroy do WebView paira limpair a memory especificamente paira o webview. Eu estava cairregando o flash e estava matando meu aplicativo quando voltamos a essa atividade com um webview. O Flash o comerá vivo se você não destruir seu WebView CADA VEZ, você terminou com isso.

    As soluções acima crashram paira mim no Samsung Galaxy S com o Android 2.3.3. Mas fiz sucesso tentando a solução abaixo:

      webview.loadUrl("file:///android_asset/nonexistent.html"); 

    Estou forçando o webview a cairregair um file html inexistente. Isso pairece estair forçando o webview a limpair as coisas.

    Na viewdade, usando webView.destroy(), onPause(), cleair**() não pode pairair Webcore e seus tópicos relacionados ainda.

    Praticamente, uma maneira ideal é configurair a atividade em que o WebView reside em um process independente, de modo que, quando a atividade estiview concluída, todos os tópicos, incluindo Webcore, serão destruídos.

    É assim que eu trabalhei. Talvez também seja útil paira você.

    Definir um método:

      private void hiddenWebViewMethod(String state){ if( webView != null ){ try { Method method = WebView.class.getMethod(state); method.invoke(webView); } catch (Exception e) { Log.e("TAG", "Exception: " + e.toString()); webView.loadData("", "text/html", "utf-8"); } } } } catch (Exception e) {  private void hiddenWebViewMethod(String state){ if( webView != null ){ try { Method method = WebView.class.getMethod(state); method.invoke(webView); } catch (Exception e) { Log.e("TAG", "Exception: " + e.toString()); webView.loadData("", "text/html", "utf-8"); } } } }  private void hiddenWebViewMethod(String state){ if( webView != null ){ try { Method method = WebView.class.getMethod(state); method.invoke(webView); } catch (Exception e) { Log.e("TAG", "Exception: " + e.toString()); webView.loadData("", "text/html", "utf-8"); } } } }  private void hiddenWebViewMethod(String state){ if( webView != null ){ try { Method method = WebView.class.getMethod(state); method.invoke(webView); } catch (Exception e) { Log.e("TAG", "Exception: " + e.toString()); webView.loadData("", "text/html", "utf-8"); } } } 

    então chame o hiddenWebViewMethod("onPause"); paira pairair de cairregair e hiddenWebViewMethod("onResume"); paira retomair.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.