首先需要编写一个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
- <plugin name="CallActivityPlugin" value="com.example.plugin.CallActivityPlugin"/>
接着利用js调用插件(callActivity.js)
- window.callActivityPlugin = function(str,callback) {
- cordova.exec(callback, pluginFailed, "CallActivityPlugin", "call", [ str ]);
- };
- var pluginFailed = function(message) {
- alert("failed>>" + message);
- }
- $(function() {
- init();
- });
- var init = function() {
- console.log("phonegap init!!");
- document.addEventListener("deviceready", onDeviceReady, true);
- }
- var onDeviceReady = function() {
- console.log("deviceready event fired");
- window.callActivityPlugin("com.example.activity.MyActivity" , function(echoValue) {
- console.log("callActivityPlugin echo>>");
- });
- };
最后html页面加载(相对简单)
- <!DOCTYPE html>
- <html>
- <head>
- <title>Device Properties Example</title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
- <script type="text/javascript" charset="utf-8" src="../js/jquery-1.8.3.js"></script>
- <script type="text/javascript" charset="utf-8" src="../js/cordova-2.3.0.js"></script>
- <script type="text/javascript" charset="UTF-8" src="../js/callActivity.js"></script>
- </head>
- <body>
- <p id="callActivityPlugin">callActivityPlugin</p>
- <a href="../index.html">返回</a>
- </body>
- </html>
新的Activity代码(相对简单)
- 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();
- }
- });
- }
- }