码迷,mamicode.com
首页 > 移动开发 > 详细

phonegap android插件,启动activity并返回值

时间:2015-09-05 08:46:01      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
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,
im 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

 

phonegap android插件,启动activity并返回值

标签:

原文地址:http://www.cnblogs.com/wdfrog/p/4782706.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!