zoukankan      html  css  js  c++  java
  • phonegap android插件,启动activity并返回值

    Your execute menthod is not quite right. When you do:
        return new PluginResult(PluginResult.Status.OK,resultFunction);
    
    that effectively returns nothing as a result. Instead you need to do:
    
        PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
        r.setKeepCallback(true);
        return r;
    
    which will tell the JS layer that there is info coming soon. Then in
    your onActivityResult method you need to call:
    
      this.success(new PluginResult(PluginResult.Status.OK, msg), this.callbackId);
    
    and you should get your result correctly.
    
    Check out the:
    View Code

    有问题的代码,参考上面修改

    Hi,
    i'm trying to pass a value from my Android java plugin to javascript in index.html.
    This is my code :
    
    PluginWrapper.js
    var PluginWrapper = function() {
    
    };
    
    PluginWrapper.prototype.crop = function (name, win, fail){
    
       console.log("Prima di execute!");
    
       return PhoneGap.exec(win,fail,"PluginWrapper","crop",[name]);
    
      };
    
    PhoneGap.addConstructor(function() {
    
     PhoneGap.addPlugin('PluginWrapper',new PluginWrapper());
    
    });
    
    PluginWrapper.java
    
    package it.Prova;
    
    
    
    import org.json.JSONArray;
    
    import com.phonegap.api.Plugin;
    
    import com.phonegap.api.PluginResult;
    
    import android.content.Intent;
    
    import android.util.Log;
    
    
    
    public class PluginWrapper extends Plugin{
    
        private String resultFunction = null;    
    
        @Override
    
        public PluginResult execute(String arg0, JSONArray arg1, String arg2) {
    
            Log.v("PLUGIN","PLUGIN EXECUTE");
    
            Start("");
    
            return new PluginResult(PluginResult.Status.OK,resultFunction);
    
        }
    
        public String Start(String name){
    
            Log.v("START","START");
    
            Intent intent = new Intent(this.ctx,PluginActivity.class);
    
            this.ctx.startActivityForResult((Plugin) this, intent, 0);
    
            return "OK";
    
        }
    
        public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    
            super.onActivityResult(requestCode, resultCode, intent);
    
            if (requestCode == 0){
    
                String msg = intent.getStringExtra("returnedData");
    
                Log.v("FLAG","IN WRAPPER " + msg);
    
                resultFunction = msg;
    
            }
    
        }
    
    }
    
    PluginActivity.java
    
    package it.Prova;
    
    import android.app.Activity;
    
    import android.content.Context;
    
    import android.content.Intent;
    
    import android.os.Bundle;
    
    import android.widget.Button;
    
    import android.util.Log;
    
    import android.view.View;
    
    import android.view.View.OnClickListener;
    
    
    
    public class PluginActivity extends Activity{
    
        private Button btn;
    
        private int flag = 0;
    
        private Intent intentNew = null;
    
        private Context context = this;
    
        
    
         @Override
    
            public void onCreate(Bundle savedInstanceState) {
    
                super.onCreate(savedInstanceState);
    
                setContentView(R.layout.main);        
    
                intentNew = this.getIntent();
    
                btn = (Button) findViewById(R.id.button1);
    
                btn.setOnClickListener(new OnClickListener(){
    
                    public void onClick(View v) {
    
                        flag = 1;
    
                        Log.v("FLAG","1 IN PLUGIN");
    
                                    intentNew.putExtra("returnedData", Integer.toString(flag));
    
               if (getParent() == null) {
                   setResult(RESULT_OK, intentNew);
               }
               else {    
                   getParent().setResult(RESULT_OK, intentNew);
               }
               finish();
                           }                
                        });
    
         }
    
    }
    
    index.html
    
    <!DOCTYPE HTML>
    
    <html>
    
    <head>
    
    <title>PhoneGap</title>
    
    <script type="text/javascript" charset="utf-8" src="phonegap-1.3.0.js"></script>
    
    <script type="text/javascript" charset="utf-8" src="PluginWrapper.js"></script>
    
    <script type="text/javascript" charset="utf-8">
    
      
    
       function onLoad(){
    
        document.addEventListener("deviceready", onDeviceReady, true);
    
       }
    
       
    
       function onDeviceReady() {
    
       //alert("OK");   
    
       }
    
       
    
       function getStart() {
    
       //window.plugins.PluginWrapper.crop("",function(r){console.log("Value in javascript " + r);},
    
    //                    function(e){console.log("NO");}
    
    //                    );
    
    
    var a = "vuoto";
    
    a = window.plugins.PluginWrapper.crop("");       
    
    console.log("a = " + a);             
    
       }
    
       
    
      </script>
    
    
    </head>
    
    <body onload="onLoad()">
    
    <h1>Hello World</h1>
    
    <button onclick="getStart();">Start</button> <br>
    
    </body>
    
    </html>
    
    
    Attach the source code!
    
    Best regards,
    
    akus85
    View Code

    继承CordovaPluging的代码,Pluging继承CordovaPluging

    public class WXPayPlugin extends CordovaPlugin {
    
        public static final String ACTION = "call";  
        private CallbackContext _callbackContext=null;
      
        @Override  
        public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {  
            if (action.equals(ACTION)) {  
                try {    
                    //下面两句最关键,利用intent启动新的Activity  
                   Intent intent = new Intent().setClass(cordova.getActivity(), Class.forName(args.getString(0)));  
                    this.cordova.startActivityForResult(this, intent, 1);  
                    
                    _callbackContext=callbackContext;
                    //下面三句为cordova插件回调页面的逻辑代码  
                    PluginResult mPlugin = new PluginResult(PluginResult.Status.NO_RESULT);  
                    mPlugin.setKeepCallback(true); 
                    callbackContext.sendPluginResult(mPlugin);  
     
                  
                      
                } catch (Exception e) {    
                    e.printStackTrace();  
                    return false;  
                }  
            }  
      
            return true;  
        }  
        //onActivityResult为第二个Activity执行完后的回调接收方法  
        @Override  
        public void onActivityResult(int requestCode, int resultCode, Intent intent){    
            switch (resultCode) { //resultCode为回传的标记,我在第二个Activity中回传的是RESULT_OK  
                case Activity.RESULT_OK:  
                    Bundle b=intent.getExtras();  //data为第二个Activity中回传的Intent  
                    String str=b.getString("change01");//str即为回传的值  
                    _callbackContext.success(str);   
                    break;  
                default:  
                    _callbackContext.error("fail");
                   break;  
            }  
          
         
        }
    
    }
    View Code
  • 相关阅读:
    1.1 创造性和动机
    【普及分享】云计算=/=云主机,阿里云=低价低端
    【大话设计模式】——策略模式
    Android 学习笔记之Bitmap位图的缩放
    Android 学习笔记进阶十二之裁截图片
    Android 学习笔记进阶14之像素操作
    Android 给图片 加边框
    android 图片特效处理之 光晕效果
    android 图片特效处理之 图片叠加
    android 图片特效处理之光晕效果
  • 原文地址:https://www.cnblogs.com/wdfrog/p/4782706.html
Copyright © 2011-2022 走看看