如何运行科尔多瓦插件在Android的后台服务? [英] How to run cordova plugin in Android background service?
问题描述
我的工作在科尔多瓦开发的移动应用程序。我想要实现不喜欢syncronise本地数据库打开套接字连接,与一台远程工作的一些后台服务和新的远程推送等通知用户。关键是我在实现这个code我想执行它,我的背景JavaScript,但
I am working on mobile application developed on cordova . I want to implement a background service that do some work like open socket connection syncronise local database with remote one and notify the users on new remote pushes etc . The point is I have this code implemented in javascript but I want execute it i background.
我搜索上网科尔多瓦后台服务插件。
I searched internet for a cordova background service plugin.
- <一个href="https://github.com/katzer/cordova-plugin-background-mode">katzer/cordova-plugin-background-mode
- jocull / PhoneGap的-backgroundjs
- katzer/cordova-plugin-background-mode
- jocull/phonegap-backgroundjs
red-folder
最好的一个,我认为是红色文件夹但它仅仅是为Android,它不会让我写javascript在后台执行。但只能兑换Java和JavaScript的JSON。
The best one I think is red-folder but it is just for android and it does not let me to write javascript to be executed in background. but just exchange json between java and javascript.
我看了一下后台服务的一些话题的android这些都是有用的,我发现:
I have read some topics about background service in android these are useful ones I found:
- <一个href="http://www.tipsfromsiliconvalley.com/2013/07/06/create-a-service-on-android-with-phonegap-application/">create-a-service-on-android-with-phonegap-application
- <一个href="http://techblogon.com/simple-android-service-example-$c$c-description-start-stop-service/">simple-android-service-example-$c$c-description-start-stop-service
- <一个href="http://stackoverflow.com/questions/18865035/android-using-webview-outside-an-activity-context">android-using-webview-outside-an-activity-context
- create-a-service-on-android-with-phonegap-application
- simple-android-service-example-code-description-start-stop-service
- android-using-webview-outside-an-activity-context
于是我开始写科尔多瓦插件(主要是在Android),以执行JavaScript code的背景。我创建了一个web视图从后台服务,从它执行JavaScript。这工作得很好,当我执行正常的JavaScript,但是当涉及到科尔多瓦的插件JS失败例如设备 device.uuid
给空
。
So I started writing cordova plugin (primarily on android) to execute the javascript code in background. I created a webview from the background service to execute the javascript from it. This works fine when I execute normal javascript but when it comes to cordova plugins js it fails for example the device device.uuid
gives null
.
这是Java服务code:
This is the java service code:
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(
android.view.ViewGroup.LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT
));
WebView wv = new WebView(this);
wv.setLayoutParams(new LinearLayout.LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT,
android.view.ViewGroup.LayoutParams.MATCH_PARENT
));
view.addView(wv);
wv.getSettings().setJavaScriptEnabled(true);
wv.setWebChromeClient(new WebChromeClient());
wv.loadUrl("file:///android_asset/www/background.html");
wv.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(final WebView view, int errorCode,
String description, final String failingUrl) {
Log.d("Error","loading web view");
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
windowManager.addView(view, params);
}
更新 有在logcat中没有错误。 于是,我试着写设备对象在屏幕上,这就是我所得到的:
Update There is no error in the logcat. So I tried to write the device object on the screen and thats what I get :
document.write(JSON.stringify(window.device))
这是结果:
{ available : false,
plaform : null ,
version : null ,
uuid : null ,
cordova : null ,
model : null
}
我试图用 cordovaWebView
但是,相同的结果将给出取代标准的 web视图
。
I tried to replace the standard webView
with cordovaWebView
But the same result is given.
//WebView wv = new WebView(this); Commented out
CordovaWebView wv = new CordovaWebView(this);
任何有关此问题的帮助?
Any help about this problem ?
推荐答案
您应该使用嵌入式科尔多瓦的WebView,而不是标准的WebView。一个标准的WebView没有设置处理科尔多瓦插件,并且设备的信息是一个插件。
You should use an embedded Cordova WebView, not a standard WebView. A standard WebView is not set up to handle Cordova plugins, and the device info is a plugin.
见<一href="http://cordova.apache.org/docs/en/3.5.0/guide_platforms_android_webview.md.html#Android%20WebViews"相对=nofollow>科尔多瓦在嵌入webviews 的文档。
这篇关于如何运行科尔多瓦插件在Android的后台服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!