最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Passing a Javascript Variable to Android Activity? - Stack Overflow

programmeradmin0浏览0评论

Basically I want get data I already have accessed from javascript and passing it to Java/Android so that I can work with it there.

     /* An instance of this class will be registered as a JavaScript interface */
    class MyJavaScriptInterface {


        @SuppressWarnings("unused")
        public void setX(String html){
            Activity.this.x = html;
            Toast.makeText(myApp, Activity.this.x, Toast.LENGTH_LONG).show();
        }

    }

this works but I want to be able to call the same Toast line anywhere and get the same result. Currently it only returns null/empty when not called through loading through webview.loadUrl("Javascript:"...

Any tips?

Basically I want get data I already have accessed from javascript and passing it to Java/Android so that I can work with it there.

     /* An instance of this class will be registered as a JavaScript interface */
    class MyJavaScriptInterface {


        @SuppressWarnings("unused")
        public void setX(String html){
            Activity.this.x = html;
            Toast.makeText(myApp, Activity.this.x, Toast.LENGTH_LONG).show();
        }

    }

this works but I want to be able to call the same Toast line anywhere and get the same result. Currently it only returns null/empty when not called through loading through webview.loadUrl("Javascript:"...

Any tips?

Share Improve this question asked Jul 2, 2012 at 4:32 KigenKigen 1232 silver badges9 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

You can not access stored javascript variables, you must do it through a function call. You have to call it from javascript in your html page, for example:

TheNameOfYourInterface.setX('value');

TheNameOfYourInterface will be a javascript object when you add the interface to the webview via

YourWebView.addJavascriptInterface(new MyJavaScriptInterface(),"TheNameOfYourInterface");

so you can do the logic on your webview and call the interface when you set the data so the method in the Java side will be called.

I found a different solution to what I need, and I feel like it works for all who do not need get a value calc'd by javascript. My solution to my need was to HTTP GET the html/javascript and then parse it as a string. It saves me some time by not needing to load X WebViews and then (re)creating all my functions in the JavaScript Interface.

I think it will be a good idea to store the HTML in Shared Preferences ,which is a form of persistent storage.This way you will be able to access it from anywhere.

   //------get sharedPreferences

SharedPreferences pref = context.getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);

//--------modify the value

pref.edit().putString("ToastString", html).mit();

//-------get a value from this from anywhere

String toastValue=pref.getString("ToastString", "");
发布评论

评论列表(0)

  1. 暂无评论