从java代码调用javascript函数(cordovaActivity)android

时间:2015-04-27 12:49:20

标签: java android angularjs cordova ionic

您好我是Android的新手并且正在使用cordova开发应用程序。同时我被迫在native和angularjs之间切换,好的是我可以从javascript代码调用java方法,但我不能实现逆转。这是我的android代码

public class CordovaApp extends CordovaActivity
{

     JavaScriptInterface javaScriptToJava = new JavaScriptInterface(this);


@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    super.init();

    appView.getSettings().setJavaScriptEnabled(true);
    appView.addJavascriptInterface(javaScriptToJava, "javaScriptToJava");
    // Set by <content src="index.html" /> in config.xml
    loadUrl(launchUrl);
}

//on ActivityResult method
public void onActivityResult(int requestCode, int resultCode,Intent intent)       {

    if (requestCode == 0) {
        Log.d("outside","on ----------------------");
        if(resultCode == RESULT_OK) {
            //get the extras that are returned from the intent
            String contents = intent.getStringExtra("SCAN_RESULT");
            String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
            Log.d("inside on activity result",contents);


        }
    }

    if(requestCode==1)
    {
        Log.d("onactivity result","got into second activity");
            int position=intent.getIntExtra("Message",0);
            Log.d("inside on activity result",""+position);
            //appView.loadUrl("javascript:alert(javaScriptToJava.startScanner())");

            //this is working-----------
            //appView.loadUrl("javascript:(function(){document.getElementById('signin').click()})()");


            //appView.loadUrl("file:///android_asset/www/index.html");
            appView.loadUrl("javascript:testFunction()");   


    }

}
}

我有两个活动,onActivityResult我想调用javascript函数但是我没有定义testFunction()并且这行正常工作

appView.loadUrl("javascript:(function({document.getElementById('signin').click()})()");

我也尝试过使用webViewClient,但这是徒劳的。在login.html和index.html中给出了脚本仍然没有定义给testFunction。 这是我在login.html中编写的示例脚本

<ion-view style="background: #8eb4e3;" >

<ion-content has-header="false"
    padding="true">
    <div class="body" >
        <div style="margin-top: 60px;" align="center">
            <img src="img/login_logo_MyStock.png" class="loginLogo">
                </div>
        <div style="margin-top: 25px;" align="center" ng-enter="login1()"  >
        </div>
        <div style="margin-top: 50px;" align="center">
            <img src="img/Tesco.png" class="tescoLogo">
                </div>
    </div>
    <script type="text/javascript">
       alert("inside script");
       function testFunction()
       {
        alert("came to testFunction");        
       }
   </script>
 </ion-content>

</ion-view>

有人可以帮忙解决这个问题吗?

0 个答案:

没有答案