简体   繁体   中英

Android - cannot acces to actionbar with JavascriptInterface

I have a strange issue with the JavascriptInterface for my WebView. I want to change the title in the actionbar with Javascript. It should happen when onPageFinished is called. Nothing happens (Sometimes there is the first letter of the new title followed by '...' ). But: When I press the Home Button and then go back into the application the title is changed. How can I instantly change the title?

In my example I call

w1.loadUrl("http://www.google.com");

in onpagefinished

 w1.loadUrl("javascript:window.jsin.abtitle(document.title)");

to change the title to the html title (in this case "Google")

@JavascriptInterface
    public void abtitle(String s){
        ((MainActivity)mContext).getActionBar().setTitle(s);
    }

After the page finished loading the title in the actionbar is "G..." Pressing the homebutton, going back into the app the title is "Google"

this is my activity:

package com.example.test;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

WebView w1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    w1=(WebView)findViewById(R.id.webView1);
    w1.getSettings().setJavaScriptEnabled(true);
    w1.setWebViewClient(wc1);
    w1.addJavascriptInterface(new MyJavascriptInterface(this), "jsin");
    w1.loadUrl("http://www.google.com");
}


public WebViewClient wc1 = new WebViewClient(){

    @Override
    public void onPageFinished(WebView view, String url) {
         w1.loadUrl("javascript:window.jsin.abtitle(document.title)");
        super.onPageFinished(view, url);
    };

};



public class MyJavascriptInterface{

Context mContext;

     public MyJavascriptInterface(Context c) {
            mContext = c;
     }

    @JavascriptInterface
    public void abtitle(String s){
        ((MainActivity)mContext).getActionBar().setTitle(s);
    }
}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}



}

this is my layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<WebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</LinearLayout>

将对 setTitle()的调用包装在Runnable中,然后调用runOnUiThread()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM