K
K
Konstantin Dovnar2014-09-09 22:51:04
Java
Konstantin Dovnar, 2014-09-09 22:51:04

Is it possible to use one Service for different tasks?

I update the widget with Service every 24 hours.
Such a thing is that it is necessary to do one more action (namely, notifications [notification]) once every 24 hours, but with a different reference point. Those. the widget is updated at 00:01, and the notification, for example, should appear at 17:40.
At the moment I am implementing through two services, which is not very beautiful and dreary in itself. Especially because the data for Notification is in the first service, so I call the first service, and from it the second one. It turns out not at all a beautiful software centipede.
Is it possible to somehow make one Service work for these two tasks, or is there nothing better than what we have now? :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
linreal, 2014-09-12
@linreal

You can use pendingIntent with different requestCode. For example:

public static void startProgramNotifications(Context c) {
        AlarmManager am;

        am = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(c, TimerReceiver.class); //TimerReceiver - сервис
        PendingIntent pendingIntent = PendingIntent.getBroadcast(c, 0, // 0 - requestCode
                intent, PendingIntent.FLAG_CANCEL_CURRENT );
        am.cancel(pendingIntent); //cancel active intent, optional
        am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ setNitifTime(), pendingIntent);

    }

In the service, in the onReceive method, we pull out the requestCode and execute the necessary code based on it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question