Mehrdad Mehrdad - 5 months ago 18
Android Question

Android Keep a process run in background

I know we can use

Services
and
IntentServices
, But I have requirement that a class keeps running and never gets removed but garbage collector.

To describe more of the reason : I'm using an event system.

So when an activity or service fires an event the central event class catches that event and does whatever needed.

How can I create a class to accomplish this task? I know the class which extends
Application
and added to manifest is one way.

But I'm looking for a better way.

Answer

But I have requirement that a class keeps running and never gets removed but garbage collector.

If you ask about "class" in general, you can use a static variable. It will never be garbage collected unless you assign null to it because a class object will hold a strong reference to it.

So when an activity or service fires an event the central event class catches that event and does whatever needed.

Use a BroadcastReceiver component for this. It was designed exactly for this purpose.

I don't see how your first question is related to the second one though.. I suppose you want your receiver to always handle events. So in this case just register it in AndroidManifest.xml so that it will run every time event occurs.