Cordova调用Activity

Cordova调用Activity,第1张

概述首先需要编写一个CallActivityPlugin插件,专门调用Activity  Java代码  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.xmlXml代码  <plugin name="CallActivityPlugin" value="com.example.plugin.CallActivityPlugin"/>   接着利用js调用插件(callActivity.js) 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页面加载(相对简单)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代码(相对简单)Java代码  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();              }          });        }  }  

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

 

 

 com.example.plugin;     org.apache.cordova.API.CallbackContext;   org.apache.cordova.API.CordovaPlugin;   org.apache.cordova.API.PluginResult;   org.Json.JsONArray;   org.Json.JsONException;     androID.app.Activity;   androID.content.Intent;   androID.os.Bundle;   androID.util.Log;      CallActivityPlugin  CordovaPlugin {         String ACTION = ;                    execute(String action, JsONArray args, CallbackContext callbackContext)  JsONException {           (action.equals(ACTION)) {               {                      Intent intent =  Intent().setClass(cordova.getActivity(), Class.forname(args.getString()));                  .cordova.startActivityForResult(, intent, );                                    PluginResult mPlugin =  PluginResult(PluginResult.Status.NO_RESulT);                  mPlugin.setKeepCallback();                    callbackContext.sendpluginResult(mPlugin);                  callbackContext.success();                                 }  (Exception e) {                    e.printstacktrace();                   ;              }          }             ;      }                    onActivityResult( requestCode,  resultCode, Intent intent){             (resultCode) {                Activity.RESulT_OK:                  Bundle b=intent.getExtras();                    String str=b.getString();                  ;              :                 ;          }      }   }  

 

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

 = =  

 

接着利用Js调用插件(callActivity.Js)

 

window.callActivityPlugin = (str,callback) {      cordova.exec(callback, pluginFailed, , [ str ]);  };     pluginFailed = (message) {      alert(>" + message);  }    $(() {      init();  });     init = () {      console.log();      document.addEventListener(, onDeviceReady, );  }     onDeviceReady = () {            console.log();        window.callActivityPlugin( , (echovalue) {          console.log(>");      });  };  

 

最后HTML页面加载(相对简单)

  
          Device PropertIEs Example   = =       = = =       = = =       = = =               =callActivityPlugin       =返回        

 

 

新的Activity代码(相对简单)

 com.example.activity;     androID.app.Activity;   androID.content.Context;   androID.content.Intent;   androID.net.Uri;   androID.os.Bundle;   androID.util.Log;   androID.vIEw.VIEw;   androID.vIEw.VIEw.OnClickListener;   androID.Widget.button;     com.example.ask.R;      MyActivity  Activity {       button btn;          flag = ;         Intent intentNew = ;         Context context = ;                onCreate(Bundle savedInstanceState) {            .onCreate(savedInstanceState);            setContentVIEw(R.layout.my_activity_main);            intentNew = .getIntent();            btn = (button) findVIEwByID(R.ID.button1);            btn.setonClickListener( OnClickListener() {                  onClick(VIEw v) {                  Intent mIntent =  Intent();                    mIntent.putExtra();                    mIntent.putExtra();                                      setResult(RESulT_OK, mIntent);                  finish();              }          });        }  }   总结

以上是内存溢出为你收集整理的Cordova调用Activity全部内容,希望文章能够帮你解决Cordova调用Activity所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1149744.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存