-1

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)?

eranga
  • 519
  • 7
  • 17
  • start your service in your broadcast receiver, do not bind to it, binding is impossible – pskink Nov 04 '15 at 17:08
  • Nope I don't want to start service inside broadcast receiver, service already started/running(Its a unstoppable service). I want to connect to this service in order to send ping message(ping messages sends via the service) – eranga Nov 05 '15 at 04:28
  • yes i know, and the only way is to call `startService` and get notified in `onStartCommand`, as i said you cannot call `bindService` from the broadcast receiver – pskink Nov 05 '15 at 09:04

1 Answers1

0

i use AlarmManager for my update functions. Maybe this works for you too.

void updatecheck(Context context){

    Intent alarmIntent = new Intent(context, yourclass.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), 30 * 60 * 1000, pendingIntent);
}
Riksor
  • 3
  • 1
  • Problem is I cannot user broadcast receiver in a separate class, since my ping sending function in remote service, If there is a way to use broadcast receiver inside my remote service class it would solve the problem. – eranga Nov 04 '15 at 17:00
  • Maybe Handling the broadcast intents?, http://stackoverflow.com/questions/9128103/broadcastreceiver-with-multiple-filters-or-multiple-broadcastreceivers – Riksor Nov 04 '15 at 17:17