phonegap3.0配置

本文详细介绍了如何在 PhoneGap 应用中调用本地的 Activity 插件,包括配置步骤、编写 JavaScript 接口、XML 配置文件以及 Java 实现跳转等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Phonegap 调用本地的Activity插件

查看我上一篇插件开发的文章:

本节主要记录调用Activity的方式; 并提供 插件Demo下载

插件开发4个步骤:

1 在assents 目录下的 cordova-plugins.js文件添加配置

2 在assets/www 的 plugin目录下编写javascript接口

3 在res/xml 目录下配置 config.xml 文件

4 在src目录下编写java文件 实现跳转

工程:

\

1 配置 cordova _plugins.js 文件

首先给大家展示cordova _plugins.js 文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
cordova.define( 'cordova/plugin_list' , function(require, exports, module) {
module.exports = [
     {
         "file" : "plugins/org.apache.cordova.camera/www/CameraConstants.js" ,
         "id" : "org.apache.cordova.camera.Camera" ,
         "clobbers" : [
             "Camera"
         ]
     },
     {
         "file" : "plugins/org.apache.cordova.camera/www/CameraPopoverOptions.js" ,
         "id" : "org.apache.cordova.camera.CameraPopoverOptions" ,
         "clobbers" : [
             "CameraPopoverOptions"
         ]
     },
     {
         "file" : "plugins/org.apache.cordova.camera/www/Camera.js" ,
         "id" : "org.apache.cordova.camera.camera" ,
         "clobbers" : [
             "navigator.camera"
         ]
     },
     {
         "file" : "plugins/org.apache.cordova.camera/www/CameraPopoverHandle.js" ,
         "id" : "org.apache.cordova.camera.CameraPopoverHandle" ,
         "clobbers" : [
             "CameraPopoverHandle"
         ]
     },
     {
         "file" : "plugins/org.apache.cordova.dialogs/www/notification.js" ,
         "id" : "org.apache.cordova.dialogs.notification" ,
         "merges" : [
             "navigator.notification"
         ]
     },
     {
         "file" : "plugins/org.apache.cordova.dialogs/www/android/notification.js" ,
         "id" : "org.apache.cordova.dialogs.notification_android" ,
         "merges" : [
             "navigator.notification"
         ]
     },
     {
         "file" : "plugins/org.apache.cordova.vibration/www/vibration.js" ,
         "id" : "org.apache.cordova.vibration.notification" ,
         "merges" : [
             "navigator.notification"
         ]
     },
     {
         "file" : "plugins/intent.js" ,
         "id" : "org.apache.cordova.intent" ,
         "merges" : [
             "navigator.intent"
         ]
     },
];
module.exports.metadata =
// TOP OF METADATA
{
     "org.apache.cordova.camera" : "0.2.7" ,
     "org.apache.cordova.dialogs" : "0.2.6" ,
     "org.apache.cordova.vibration" : "0.3.7" ,
     "org.apache.cordova.intent" : "0.0.1" ,
 
}
// BOTTOM OF METADATA
});

2 在plugin目录下编写javascript接口


\

贴上intent.js的接口代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
cordova.define( "org.apache.cordova.intent" , function(require, exports, module) {
 
var exec = require( 'cordova/exec' );
 
 
 
module.exports = {
 
     /**
      * 一共5个参数
        第一个 :成功会掉
        第二个 :失败回调
        第三个 :将要调用的类的配置名字(在config.xml中配置 稍后在下面会讲解)
        第四个 :调用的方法名(一个类里可能有多个方法 靠这个参数区分)
        第五个 :传递的参数  以json的格式
      */
     demo: function(mills) {
         exec(function(winParam){
             alert(winParam);
         }, null , "Demo" , "intent" , [mills]);
     },
};
 
});


Demo中成功返回 会弹出一个Alert();

<3> 在res/xml 目录下配置 config.xml 文件

\

config.xml配置 加上 如下

?
1
2
3
<feature name= "Demo" >
     <param name= "android-package" value= "plugins.Plugin_intent" >
</feature>


feature的name属性 非常重要

name必须是步骤< 4 >中 function中调用的类名

value属性指定插件在src目录下的java文件 (命名空间)

前3个步骤 参考上一章 phonegap插件开发

这里主要描述

<4> 编写phonegap的java插件类 调用本地的activity

首先继承CordovaPlugin

重写execute方法 execute返回值为true时 此插件可用;返回为false时 此插件失效

在插件类当中获取 this 跳转

?
1
2
Intent intent = new Intent(cordova.getActivity(), DemoActivity. class );
cordova.startActivityForResult((CordovaPlugin) this ,intent, 200 );


如果使用

?
1
cordova.getActivity().startActivityForResult(intent, 200 );

被调用的Activity 在 setResult之后;

并不会返回到当前的插件中 它将返回到的webView的CordovaActivity当中 ,

Plugin_intent 类的 onActivityResult将收不到消息 ;

我查看源码后得知 cordova这个是CordovaInterface类型的 已由CordovaPlugin实现

\

在CordovaInterface当中找到了

?
1
2
3
4
5
6
7
8
9
/**
     * Launch an activity for which you would like a result when it finished. When this activity exits,
     * your onActivityResult() method will be called.
     *
     * @param command     The command object
     * @param intent      The intent to start
     * @param requestCode   The request code that is passed to callback to identify the activity
     */
    abstract public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode);


完整实例

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package plugins;
 
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
 
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
 
import com.phonegap.helloworld.DemoActivity;
 
public class Plugin_intent extends CordovaPlugin {
 
     /**
      * 注意 构造方法不能为
      *
      * Plugin_intent(){}
      *
      * 可以不写或者 定义为如下
      *
      */
     public Plugin_intent() {
     }
 
     CallbackContext callbackContext;
 
     @Override
     public boolean execute(String action, JSONArray args,
             CallbackContext callbackContext) throws JSONException {
         this .callbackContext = callbackContext;
         Log.i( "123" , action);
 
         if (action.equals( "intent" )) {
             // 获取args的第一个参数
             String message = args.getString( 0 );
             this .function();
             return true ;
         }
         return false ;
 
     }
 
     private void function() {
         // cordova.getActivity() 获取当前activity的this
         Log.i( "123" , cordova.getActivity().toString());
         Intent intent = new Intent(cordova.getActivity(), DemoActivity. class );
         cordova.startActivityForResult((CordovaPlugin) this ,intent, 200 );
 
     }
 
     @Override
     public void onActivityResult( int requestCode, int resultCode, Intent intent) {
         
         super .onActivityResult(requestCode, resultCode, intent);
         //传递返回值 给js方法
         callbackContext.success( "activity 跳转成功了" );
         Toast.makeText(cordova.getActivity(), "123" , 0 ).show();
 
     }
}


提供下载Demo下载地址: 审核中.....

工程下载 将phonegap的platforms导入到eclipse中

如果报错clear一下 查看导的lib包 有没有报错

如果还有错 那么就是您选用了 google的API 改成最新版的android API 就好了

如果导入工程遇到问题 可以查阅我此篇文章

内容概要:该论文探讨了一种基于粒子群优化(PSO)的STAR-RIS辅助NOMA无线通信网络优化方法。STAR-RIS作为一种新型可重构智能表面,能同时反射和传输信号,与传统仅能反射的RIS不同。结合NOMA技术,STAR-RIS可以提升覆盖范围、用户容量和频谱效率。针对STAR-RIS元素众多导致获取完整信道状态信息(CSI)开销大的问题,作者提出一种在不依赖完整CSI的情况下,联合优化功率分配、基站波束成形以及STAR-RIS的传输和反射波束成形向量的方法,以最大化总可实现速率并确保每个用户的最低速率要求。仿真结果显示,该方案优于STAR-RIS辅助的OMA系统。 适合人群:具备一定无线通信理论基础、对智能反射面技术和非正交多址接入技术感兴趣的科研人员和工程师。 使用场景及目标:①适用于希望深入了解STAR-RIS与NOMA结合的研究者;②为解决无线通信中频谱资源紧张、提高系统性能提供新的思路和技术手段;③帮助理解PSO算法在无线通信优化问题中的应用。 其他说明:文中提供了详细的Python代码实现,涵盖系统参数设置、信道建模、速率计算、目标函数定义、约束条件设定、主优化函数设计及结果可视化等环节,便于读者理解和复现实验结果。此外,文章还对比了PSO与其他优化算法(如DDPG)的区别,强调了PSO在不需要显式CSI估计方面的优势。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值