JavascriptInterface函数在网页上崩溃javascript

时间:2015-11-04 17:25:40

标签: javascript java android

我的Android应用程序中有一个webview。我只想在浏览器上查看网页时显示横幅。我创建了一个界面,它测试很好,但因为网页没有识别该功能,页面上的其他JavaScript无法加载。这是我的代码:

接口类

import android.content.Context;
import android.webkit.JavascriptInterface;


public class WebAppInterface {
Context mContext;

/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
    mContext = c;
}

/** Show a toast from the web page */
@JavascriptInterface
public Boolean getUser() {

    return true;
}
}

活动类

public class MainActivity extends Activity {


    ProgressBar progressBar;
    WebView webview;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);





        setContentView(R.layout.activity_main);

        webview = (WebView)findViewById(R.id.webview);
        progressBar = (ProgressBar)findViewById(R.id.progressBar);
        webview.addJavascriptInterface(new WebAppInterface(this),"Android");
        this.webview.getSettings().setUserAgentString(
                this.webview.getSettings().getUserAgentString()
                        + " "
                        + getString(R.string.user_agent_suffix)
        );
        WebSettings settings = webview.getSettings();
        settings.setJavaScriptEnabled(true);


        webview.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                progressBar.setProgress(progress);
                if (progress == 100) {
                    progressBar.setVisibility(View.GONE);

                } else {
                    progressBar.setVisibility(View.VISIBLE);

                }
            }
        });





        webview.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                view.loadUrl(url);
                return true;
            }


        });
        webview.loadUrl("http://www.getonfleek.com");
        ParsePush.subscribeInBackground("test");
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    @Override
    public void onBackPressed() {
        if (webview.canGoBack()) {
            webview.goBack();
        } else {
            super.onBackPressed();
        }
    }




}

HTML

</script>
    <script type="text/javascript">
    $(document).ready(function(){
        var user = Android.getUser();
      if(user == null){
        //do something on web
}else{
//do something in app
}
});
</script>

1 个答案:

答案 0 :(得分:1)

在尝试调用之前检查以确保接口存在。

<script type="text/javascript">
    $(document).ready(function(){
        var user = null;
        if(typeof Android !== 'undefined'){
            user = Android.getUser();
            //do something in app
        }else{
            //do something on web
        }
    });
</script>