首先需要编写一个CallActivityPlugin插件,专门调用Activity
package com.example.plugin;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;
import org.json.JSONException;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class CallActivityPlugin extends CordovaPlugin {
public static final String ACTION = "call";
@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);
//下面三句为cordova插件回调页面的逻辑代码
PluginResult mPlugin = new PluginResult(PluginResult.Status.NO_RESULT);
mPlugin.setKeepCallback(true);
callbackContext.sendPluginResult(mPlugin);
callbackContext.success("success");
} 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即为回传的值
break;
default:
break;
}
}
} 然后配置CallActivityPlugin插件res/xml/config.xml
package com.example.activity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.example.ask.R;
public class MyActivity 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.my_activity_main);
intentNew = this.getIntent();
btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent mIntent = new Intent();
mIntent.putExtra("change01", "1000");
mIntent.putExtra("change02", "2000");
// 设置结果,并进行传送
setResult(RESULT_OK, mIntent);
finish();
}
});
}
} 参考文章:https://github.com/phonegap/phonegap-plugins/blob/master/Android/EmailComposerWithAttachments/EmailComposer.java