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??
}
}
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.
Comments