调用一个打开活动并将活动结果返回给javascript的java函数

时间:2014-08-27 05:11:27

标签: javascript android cordova android-activity webview

我在一个工作流程中遇到了问题。

我有混合应用程序。我正在调用一个java函数,它在术语打开,活动和返回。我希望将活动的返回值返回给javascript。但这里发生的是活动结束前的函数返回。我尝试使用线程但它停止渲染ui。请帮忙...... #

主要活动(Ftsbeta.java)

/ *包和导入语句* /

public class Ftsbeta extends CordovaActivity {

    String retURL="";
    public static Thread t;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.init();
        t=Thread.currentThread();
        appView.addJavascriptInterface(this, "MainActivity");
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        super.loadUrl(Config.getStartUrl());

    }


    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();

    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        }

    @JavascriptInterface
    public String customFunctionCalled(final String url) throws InterruptedException {
        Log.e("Custom Function Called", "Custom Function Called");
        t= new Thread(new Runnable() {

            @Override public void run()
            {
                // do some work here
                synchronized (t) {
                    try {
                        Intent i = new Intent(Ftsbeta.this, CustomWV.class);
                        i.putExtra("url", url);
                        startActivityForResult(i, 02);

                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        Log.e("msg","Exception");
                        e.printStackTrace();
                    }

                }

            }
        });

        t.start();
    //  Thread.currentThread().join();





        Log.e("msg","returned");
        return retURL;


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent intent) {
        // TODO Auto-generated method stub
        // check if the request code is same as what is passed here it is 2
        synchronized (t) {


        if (requestCode == 2) {
            // fetch the message String
            if(resultCode==RESULT_OK){
            retURL = intent.getStringExtra("url");
            // Set the message string in textView
            Log.e("url",retURL);

            }

        }
        Log.e("here","time to notify");
        t.notify();

        }
    }
}

# 正在打开的另一个Activity(Customwv.java) / * 进口和包装 * /

public class CustomWV extends Activity {
    private WebView webView;
    ProgressDialog d;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.custom);
        Intent myIntent = getIntent(); // gets the previously created intent
        String loadURL = myIntent.getStringExtra("url"); // will return
        Log.e("here","opening windows");                                        

        webView = (WebView) findViewById(R.id.wv1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Log.e("here","opening windows2");
                d = ProgressDialog.show(CustomWV.this, "Fts",
                        "Please Wait While Till Site Loads");
                view.loadUrl(url);
                return false;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);
                d.dismiss();
                if (url.contains("action=done")) {
                    Intent intentMessage = new Intent();

                    // put the message to return as result in Intent
                    intentMessage.putExtra("url", url);
                    // Set The Result in Intent
                    setResult(02, intentMessage);
                    finish();
                }
            }

        });
        webView.loadUrl(loadURL);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        return super.onCreateOptionsMenu(menu);

    }

}

从我调用它的Javascript。

代码是: var a = window.MainActivity.customFunctionCalled(pgurl);

请以正确的方式帮我做。

提前致谢...

1 个答案:

答案 0 :(得分:1)

Cordova插件机制的全部目的是建立native和js端之间的通信。看一下文档和echo插件示例。

http://cordova.apache.org/docs/en/3.5.0/guide_hybrid_plugins_index.md.html#Plugin%20Development%20Guide

使用插件机制等待和延迟响应等是可能的。在github中还有几个插件可以处理你指出的确切问题。