Pergunta

i am working on android app with phonegap, i want send a piece of JavaScript from native Java code like "alert('23')" to web view, but how?

Details:

Phonegap: 3.0.0

Platform: Android

i use sendJavascript function for this purpose but nothing happened and not working for me.

My code:

public class HelloWorld extends DroidGap
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // Set by <content src="index.html" /> in config.xml
        super.loadUrl(Config.getStartUrl());
        //super.loadUrl("file:///android_asset/www/index.html")
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();
        if (Intent.ACTION_SEND.equals(action) && type != null) {
            this.handleSendText(intent);
        }
    }
    
    public void handleSendText(Intent intent)
    {
        String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
        if (sharedText != null) {
            String js = String.format("callss('%s');", "hadi");

            // code to send JavaScript
            this.sendJavascript(js);
        }
    }
    
}

somewhere i read about use webView.sendJavascript instead, but webView is not accessible. if any extra information must be presented describe them.

Foi útil?

Solução

just use:

super.loadURL('javascript:myFunction()');
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top