How to have an android service keep the phonegap u

2019-07-17 16:57发布

I am working on an android application that uses phonegap/cordova. 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. The problem I was facing was that the app would eventually just quit when android was short on memory or whatsoever.

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

1条回答
我想做一个坏孩纸
2楼-- · 2019-07-17 17:22

Try registering a PendingIntent with the AlarmManager that will check to make sure your application is running every n milliseconds. 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.

查看更多
登录 后发表回答