我正在研究在cordova上开发的移动应用程序.我想实现一个后台服务,它做一些工作,比如打开套接字连接同步本地数据库和远程服务器,并通知用户新的远程推送等.关键是我在javascript中实现了这个代码,但我想在后台执行它.
我在互联网上搜索了一个cordova后台服务插件.
我认为最好的是红色文件夹,但它只适用于Android,它不允许我编写javascript以在后台执行.但只是在java和javascript之间交换json.
我已经阅读了一些关于android中后台服务的主题,这些是我发现的有用的:
所以我开始编写cordova插件(主要是在android上)来在后台执行javascript代码.我从后台服务创建了一个webview来执行它的javascript.这在我执行普通的javascript时工作正常但是当涉及到cordova插件时,它失败了,例如设备device.uuid给出的null.
这是java服务代码:
public void onStart(Intent intent, int startId) {
Toast.makeText(this, "My Happy Service Started", Toast.LENGTH_LONG).show();
createBackGroundView();
super.onStart(intent,startId);
}
public void createBackGroundView(){
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutParams params = new WindowManager.LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
PixelFormat.TRANSLUCENT
);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 0;
params.width = 200;
params.height = 200;
LinearLayout view = new LinearLayout(this);
view.setLayoutParams(new RelativeLayout.LayoutParams( …Run Code Online (Sandbox Code Playgroud) 我有一个错误,即第一次尝试时没有成功调用Camera(点击)所以我在stackoverflow上尝试了这个解决方案Phonegap(3.0.0)相机在第一次尝试时没有成功.我按照回答中提到的步骤删除android通过cordova平台删除android然后我运行第二个命令cordova平台添加android ;
现在,当我使用netbeans在cordova android decvice上运行cordova应用程序时,会发生以下错误:
exec: ant debug -f "/var/www/mobile/platforms/android/build.xml"
[ 'ant debug -f "/var/www/mobile/platforms/android/build.xml"',
{ [Error: Command failed:
BUILD FAILED
/var/www/adt-bundle-linux-x86_64-20130917/sdk/tools/ant/build.xml:720: The following error occurred while executing this line:
/var/www/adt-bundle-linux-x86_64-20130917/sdk/tools/ant/build.xml:734: Compile failed; see the compiler error output for details.
Total time: 8 seconds
] killed: false, code: 1, signal: null },
'Buildfile: /var/www/mobile/platforms/android/build.xml\n\n-set-mode-check:\n\n-set-debug-files:\n\n-check-env:\n [checkenv] Android SDK Tools Revision 22.3.0\n [checkenv]
.
.
.**LONG TEXT which I removed from the post **
.
\nBUILD FAILED\n/var/www/adt-bundle-linux-x86_64-20130917/sdk/tools/ant/build.xml:720: …Run Code Online (Sandbox Code Playgroud) 我的服务器想在连接时向客户端发送一条消息。然后客户端向服务器发送消息。
当客户端向服务器发送消息但客户端未收到服务器应在连接上发送的初始消息时,我的代码工作正常。
这是我的小代码:)
服务器
var app = require('http').createServer(handler),
io = require('socket.io').listen(app, { log: false }),
fs = require('fs');
app.listen(8001);
function handler(req, res) {
fs.readFile(__dirname + '/client1.html', function(err, data) {
if (err) {
console.log(err);
res.writeHead(500);
return res.end('Error loading client1.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
console.log("running time");
socket.emit("serverMessage","server says hi");
socket.on('clientMessage', function () {
console.log("client message recieved");
});
});
Run Code Online (Sandbox Code Playgroud)
客户
<html>
<head>
<script src="/socket.io/socket.io.js"></script>
<script>
socket = io.connect("http://localhost:8001");
socket.on('connection', function(socket) {
socket.on("serverMessage", function(d) {
console.log("server …Run Code Online (Sandbox Code Playgroud) android ×2
cordova ×2
javascript ×2
mobile ×1
netbeans-7 ×1
node.js ×1
socket.io ×1
webview ×1