简体   繁体   English

如何使用android服务使phonegap ui保持活动状态

[英]How to have an android service keep the phonegap ui alive

I am working on an android application that uses phonegap/cordova. 我在使用phonegap / cordova的android应用程序上工作。 This app uses a javascript to fetch content from the server from time to time and issues a systemtray alert if there is new content. 此应用程序使用JavaScript时不时从服务器获取内容,并在有新内容时发出系统托盘警报。 The problem I was facing was that the app would eventually just quit when android was short on memory or whatsoever. 我面临的问题是,当android内存不足或其他原因时,该应用程序最终只会退出。

So I added a service to my manifest (everybody says a service would solve the problem). 因此,我在清单中添加了一项服务(每个人都说一项服务可以解决问题)。

<service android:name="UpdateService" />

Verywell, but just having this service there does nothing (obviously). 很好,但是仅在那里提供此服务(显然)没有任何作用。 So how do I make my service trigger the main application to keep running? 那么,如何使我的服务触发主应用程序以使其继续运行?

ADDITION: I also managed to start the service in my main application 另外:我还设法在主应用程序中启动了该服务

Try registering a PendingIntent with the AlarmManager that will check to make sure your application is running every n milliseconds. 尝试向AlarmManager注册PendingIntent,这将检查以确保您的应用程序每n毫秒运行一次。 If it isn't running have the service start your application again. 如果未运行,请再次启动该服务。

Use some code like this to start your application: 使用如下代码启动您的应用程序:

Intent serviceIntent = new Intent("com.myapp.LaunchActivity");
startService(serviceIntent);

Replace com.myapp.LaunchActivity with your package name and LaunchActivity. 与你的包名和LaunchActivity替换com.myapp.LaunchActivity。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM