I have a service which runs on remote process(via AIDL interface). It is a unstoppable service (which starts on boot complete and last until uninstalling the app). This service keep listening to UDP socket. I want to run function in this service in every 30 minutes(which responsible to send ping messages to server via udp socket).
I have tried to start thread and sleep for 30 minutes, it didnt work
new Thread(new Runnable() {
public void run() {
while (true) {
try {
sendPingMessage();
Thread.currentThread().sleep(1000 * 60 * 30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
This function calls in arbitrary amount of time. Not exactly in 30 minutes.
Also tried timer task, handler and ScheduledExecutorService, No luck with it as well
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
scheduler.scheduleAtFixedRate (new Runnable() {
public void run() {
sendPingMessage();
}
}, 0, 30, TimeUnit.MINUTES);
Seems the problem is with android deep sleep mode [http://binarybuffer.com/2012/07/executing-scheduled-periodic-tasks-in-android][1]
I have tried to use AlarmManger as well. But I couldn't use broadcast receiver for alarm manger in separate class. Since I want to send ping message from android remote service(I couldn't connect/bind to remote service inside broadcast receiver)
What could be the best approach to overcome my problem? Can I have alarm manger broadcast receiver inside my service class(with out using separate broadcast receiver class)?