Custom cordova plugin creation for ionic2 project

前端 未结 3 836
走了就别回头了
走了就别回头了 2021-01-01 01:41

Many of us would have gone through similar issues, but even after going through following most relevant links reference link1 and reference link2 , I am not able to resolve.

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-01 02:29

    Your plugin needs to look like this:

    In: /[custom plugin name]/js/custom_plugin.js

    var CustomPlugin = function(){};
    
    CustomPlugin.someFunction = function(){
        console.log("someFunction starts");
    
        return new Promise(function(resolve,reject){
        cordova.exec(
            resolve,
            reject,
            [PLUGIN_NAME],
            [ACTION_ON_NATIVE_SIDE],
            []
        );
    
        });
        console.log("someFunction stops");
    }
    
    .... more functions
    
    module.exports = CustomPlugin;
    

    In: /[custom plugin name]/src/[android]||[ios] , the classes with native code.

    And in: /[custom plugin name]/plugin.xml (this is an example, settings have to be adjusted to your case):

    
    
        CustomPlugin
        ...
        ...
        ...
    
        
            
        
    
    
    
        
            
        
    
    
        
            
                
                
                    
                    
                
            
    
            
            
            
        
    
        
            
                
                
                    
                    
                
            
    
            
                
                
                
            
    
            
            
            
    
        
    
    
    

    then you add you plugin with the CLI: ionic plugin add [folder of your plugin]

    In your Ionic project, in the classes (angular2 directives) where you want to use your plugin, write before the @Component section: declare var CustomPlugin: any;. Then in that class, you can use your plugin by refering to CustomPlugin that is exported with module.exports = CustomPlugin; from the file: /[custom plugin name]/js/custom_plugin.js.

    TO ANSWER EDIT 1 OF THE QUESTION, HERE SOME DETAILS OF THE ANDROID PART: In the android plugin project (once platform android has been added and built at least once, with ionic CLI), in android studio (2.2.2), when looking at the build project under "[my project]\platforms\android":

    In the hierarchy, the MainActivity file is autogenerated under: "android\java\com\ionicframework.[my project name + a large number]\MainActivity":

      package com.ionicframework.[my project name + a large number];
    
    import android.os.Bundle;
    import org.apache.cordova.*;
    
    public class MainActivity extends CordovaActivity
    {
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
    
            // enable Cordova apps to be started in the background
            Bundle extras = getIntent().getExtras();
            if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
                moveTaskToBack(true);
            }
    
            // Set by  in config.xml
            loadUrl(launchUrl);
        }
    }
    

    For my custom plugin (not going into details here) under "android\java[package of the custom plugin]:

    package [package of the custom plugin];
    
    import org.apache.cordova.CallbackContext;
    import org.apache.cordova.CordovaPlugin;
    import org.apache.cordova.PluginResult;
    
    // + other imports needed
    
    public class CustomPlugin extends CordovaPlugin  {
    
        private static CallbackContext customDeferredCallback;
    
        public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
    //all the thing corresponding to your case need to end if with either:
    //   if OK: callbackContext.success(); return true;  ;
    //   if not OK: callbackContext.error(error.getMessage()); return false;
    //   if JAVA returns a result to JS do: actionBindListener(callbackContext);
    
    
    
    }
    
        private boolean actionBindListener(final CallbackContext callbackContext){
        cordova.getThreadPool().execute(new Runnable() {
                public void run(){
                    try{
                        customDeferredCallback= callbackContext;
                    }catch(Exception e){e.printStackTrace();}
                }
            });
            return true;
        }
    
    
        //in your program when you get the result you want to send back to javascript call this function
        public static void sendResultToJS(res){
            JSONObject eventData = new JSONObject();
            try {
                eventData.put("CUSTOM_KEY", res);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, eventData);
            pluginResult.setKeepCallback(true);
            try{
                    customDeferredCallback.sendPluginResult(pluginResult);
            } catch(NullPointerException e){
                e.printStackTrace();
            }
        }
    }
    

    And finally android\manifests\manifests.xml looks like that:

    
    
        
        
        
            
                
                    
                    
                
            
            
            
            
                
                    
                    
                
            
            
                
                    
                
            
            
                
                    
                
            
            
        
        
        
        
        
        
        
        
        
        
    
    

提交回复
热议问题