1

The code is working on emulator but not working on real device.

classJavaScriptInterface{
    private TextView contentView;
    public JavaScriptInterface(TextView aContentView){
        contentView = aContentView;
    }
    @SuppressWarnings("unused")
    public void processContent(String aContent){
        final String content = aContent;
        contentView.post(new Runnable(){
            public void run(){
                contentView.setText(content);
                contentView.setTextColor(Color.DKGRAY);
            }
        });
    }
} 

TextView tv2 = (TextView) findViewById(R.id.source);
webview1.addJavascriptInterface(new JavaScriptInterface(tv2), "INTERFACE");

webview1.setWebViewClient(new WebViewClient(){  
    @Override   
    public void onPageFinished(WebView view, String url)   
    {   
        view.loadUrl("javascript:window.INTERFACE.processContent(document.getElementsByTagName('body')[0].innerText);");        
    }    
});  
Akshay
  • 6,029
  • 7
  • 40
  • 59
cimicimi
  • 208
  • 1
  • 2
  • 11

1 Answers1

0

In my experience, onPageFinished() is not always a reliable way to know if the page is truly finished. Depending on what you're loading, there may be additional redirects or frames that will occur after onPageFinished() is called. See this other SO post for more details: How to determine when Android WebView is completely done loading?

You can quickly check to see if this is the problem by adding a delay in onPageFinished:

@Override   
public void onPageFinished(final WebView view, String url)   
{   
    view.postDelayed(new Runnable()   
    {      
        public void run()   
        {
            view.loadUrl("javascript:window.INTERFACE.processContent(document.getElementsByTagName('body')[0].innerText);");
        }       
    }, 5000);
} 

If it works with the delay, you should use the methods described in the other post to fix the issue (don't use postDelayed with a 5s delay in your final product).

Community
  • 1
  • 1
mikejonesguy
  • 9,779
  • 2
  • 35
  • 49
  • I've tried. But does not work on my real device. I writing Student information system, i must getting data from webview. What would you should me? – cimicimi Mar 07 '14 at 09:28
  • 1
    Thank you mikejonesguy. I solved my problem. I learned i need a @JavascriptInterface, my device android 4.2 and my emulator android 4.0, project running 4.2 or more new android version. – cimicimi Mar 07 '14 at 10:19