Cordova调用Activity

首先需要编写一个CallActivityPlugin插件,专门调用Activity

 

 

 
  1. package com.example.plugin;

  2.  
  3. import org.apache.cordova.api.CallbackContext;

  4. import org.apache.cordova.api.CordovaPlugin;

  5. import org.apache.cordova.api.PluginResult;

  6. import org.json.JSONArray;

  7. import org.json.JSONException;

  8.  
  9. import android.app.Activity;

  10. import android.content.Intent;

  11. import android.os.Bundle;

  12. import android.util.Log;

  13.  
  14. public class CallActivityPlugin extends CordovaPlugin {

  15. public static final String ACTION = "call";

  16.  
  17. @Override

  18. public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

  19. if (action.equals(ACTION)) {

  20. try {

  21. //下面两句最关键,利用intent启动新的Activity

  22. Intent intent = new Intent().setClass(cordova.getActivity(), Class.forName(args.getString(0)));

  23. this.cordova.startActivityForResult(this, intent, 1);

  24. //下面三句为cordova插件回调页面的逻辑代码

  25. PluginResult mPlugin = new PluginResult(PluginResult.Status.NO_RESULT);

  26. mPlugin.setKeepCallback(true);

  27.  
  28. callbackContext.sendPluginResult(mPlugin);

  29. callbackContext.success("success");

  30.  
  31. } catch (Exception e) {

  32. e.printStackTrace();

  33. return false;

  34. }

  35. }

  36.  
  37. return true;

  38. }

  39. //onActivityResult为第二个Activity执行完后的回调接收方法

  40. @Override

  41. public void onActivityResult(int requestCode, int resultCode, Intent intent){

  42. switch (resultCode) { //resultCode为回传的标记,我在第二个Activity中回传的是RESULT_OK

  43. case Activity.RESULT_OK:

  44. Bundle b=intent.getExtras(); //data为第二个Activity中回传的Intent

  45. String str=b.getString("change01");//str即为回传的值

  46. break;

  47. default:

  48. break;

  49. }

  50. }

  51. }

 

 然后配置CallActivityPlugin插件res/xml/config.xml

 

接着利用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页面加载(相对简单)

 
  1. Device Properties Example

  2. callActivityPlugin

  3. 返回

 

 

新的Activity代码(相对简单)

 
  1. package com.example.activity;

  2.  
  3. import android.app.Activity;

  4. import android.content.Context;

  5. import android.content.Intent;

  6. import android.net.Uri;

  7. import android.os.Bundle;

  8. import android.util.Log;

  9. import android.view.View;

  10. import android.view.View.OnClickListener;

  11. import android.widget.Button;

  12.  
  13. import com.example.ask.R;

  14.  
  15. public class MyActivity extends Activity {

  16. private Button btn;

  17.  
  18. private int flag = 0;

  19.  
  20. private Intent intentNew = null;

  21.  
  22. private Context context = this;

  23.  
  24. @Override

  25. public void onCreate(Bundle savedInstanceState) {

  26.  
  27. super.onCreate(savedInstanceState);

  28.  
  29. setContentView(R.layout.my_activity_main);

  30.  
  31. intentNew = this.getIntent();

  32.  
  33. btn = (Button) findViewById(R.id.button1);

  34.  
  35. btn.setOnClickListener(new OnClickListener() {

  36.  
  37. public void onClick(View v) {

  38. Intent mIntent = new Intent();

  39. mIntent.putExtra("change01", "1000");

  40. mIntent.putExtra("change02", "2000");

  41. // 设置结果,并进行传送

  42. setResult(RESULT_OK, mIntent);

  43. finish();

  44. }

  45. });

  46.  
  47. }

  48. }

 

参考文章:https://github.com/phonegap/phonegap-plugins/blob/master/Android/EmailComposerWithAttachments/EmailComposer.java

你可能感兴趣的:(Android)