Calling a javascript function from java method in Cordova

megha

I made a plugin for communication between javascript and native java code.But due to some reason i had to return to callback immediately. And in turn,I started an asynchronous method in java.Now after it completes implementation,I want to return back to my javascript.Please tell me how to do it.

My java code is-

public class MyPlugin extends CordovaPlugin
{
    String fileName;
    Context context;
    @Override
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
        // your init code here
    }

     @Override
     public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException 
     {
            context=this.cordova.getActivity().getApplicationContext();
            String myurl = args.getString(0);
            if (action.equals("plugin1"))
            { 

                   new DownloadManager().execute(myurl);

                    callbackContext.success("Operation performed successfully!!");
                    return true;
             }


            callbackContext.error("error");
            return false;

     }

     public class DownloadManager extends AsyncTask<String, String, String> {

        @Override
        public String doInBackground(String... arg0) {
            downloadapk(arg0[0]);
            installapk();
            System.out.println("Download Complete");
            return null;
//Here I want to return to javascript.How can I callback to the javascript function from //async method?? 
        }

     }
codevision

You have to pass callbackContext to the DownloadManager class.

 @Override
 public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException 
 {
        context=this.cordova.getActivity().getApplicationContext();
        String myurl = args.getString(0);
        if (action.equals("plugin1"))
        {
            // Pass callbackContext to your actual code which performing the work.
            new DownloadManager(callbackContext).execute(myurl);
            return true;
        }

        callbackContext.error("error");
        return false;
 }

 public class DownloadManager extends AsyncTask<String, String, String> {
    private CallbackContext callbackContext;
    public DownloadManager(CallbackContext callbackContext) {
         this.callbackContext = callbackContext;
    }

    @Override
    public String doInBackground(String... arg0) {
        downloadapk(arg0[0]);
        installapk();
        this.callbackContext.success();
        return null;
        //Here I want to return to javascript.How can I callback to the javascript function from //async method?? 
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Calling Java method from JavaScript using ScriptEngine

From Dev

Call javascript function from cordova java file

From Dev

Calling a method of a function constructor from another function in the global scope - Javascript

From Dev

calling an object method with a setTimeout function from within the same object in Javascript

From Dev

Calling a method in a controller defined in a directive from a javascript function

From Dev

Calling function from setTimeout() method

From Dev

Calling a function from a class method

From Dev

calling a java servlet from javascript ajax post method

From Java

Calling Javascript function of .js file from java GWT code

From Dev

Calling JAVA Method from XQuery

From Dev

Java calling a print method from a main function and using data from another separate method

From Dev

Calling Javascript prototype method from another method

From Javascript

Calling a Javascript Function from Console

From Javascript

Calling JavaScript Function From CodeBehind

From Dev

Calling a Javascript function from .cs

From Dev

Calling JavaScript function from [WebMethod]

From Java

Calling a Method from Constructor Method - Java

From Java

calling another method from the main method in java

From Dev

Calling java function from dart

From Dev

undefined function when calling javascript prototype method

From Dev

Casting issue on calling the method of having javascript function

From Dev

Javascript calling function from inside same function

From Dev

Calling a method of a global object from a function

From Dev

Calling a function that returns Result<_,_> from a map method

From Dev

Calling a static template method from a template function

From Java

calling a java servlet from javascript

From Dev

calling GWT java method in jsni function

From Dev

JavaScript: Not calling javascript function from the code behind

From Dev

Why not to use parentheses in front of name of an external function while calling it from addEventListner method in JavaScript

Related Related

  1. 1

    Calling Java method from JavaScript using ScriptEngine

  2. 2

    Call javascript function from cordova java file

  3. 3

    Calling a method of a function constructor from another function in the global scope - Javascript

  4. 4

    calling an object method with a setTimeout function from within the same object in Javascript

  5. 5

    Calling a method in a controller defined in a directive from a javascript function

  6. 6

    Calling function from setTimeout() method

  7. 7

    Calling a function from a class method

  8. 8

    calling a java servlet from javascript ajax post method

  9. 9

    Calling Javascript function of .js file from java GWT code

  10. 10

    Calling JAVA Method from XQuery

  11. 11

    Java calling a print method from a main function and using data from another separate method

  12. 12

    Calling Javascript prototype method from another method

  13. 13

    Calling a Javascript Function from Console

  14. 14

    Calling JavaScript Function From CodeBehind

  15. 15

    Calling a Javascript function from .cs

  16. 16

    Calling JavaScript function from [WebMethod]

  17. 17

    Calling a Method from Constructor Method - Java

  18. 18

    calling another method from the main method in java

  19. 19

    Calling java function from dart

  20. 20

    undefined function when calling javascript prototype method

  21. 21

    Casting issue on calling the method of having javascript function

  22. 22

    Javascript calling function from inside same function

  23. 23

    Calling a method of a global object from a function

  24. 24

    Calling a function that returns Result<_,_> from a map method

  25. 25

    Calling a static template method from a template function

  26. 26

    calling a java servlet from javascript

  27. 27

    calling GWT java method in jsni function

  28. 28

    JavaScript: Not calling javascript function from the code behind

  29. 29

    Why not to use parentheses in front of name of an external function while calling it from addEventListner method in JavaScript

HotTag

Archive