Help me find out how to use services and themes

I ask for help, so my life, and more importantly, my user’s life will not be destroyed by me, not knowing how to use the services and flows correctly.

I do not ask for a long explanation, but more confirmation. This is normal if I am wrong. I am here to study.

If I understand correctly: 1. The service runs in the background (without an interface). 2. Theoretically, the service will work forever until it kills itself (I take a big guess here) 3. the service will continue to work, even if the main activity is not visible (how about even destroyed?)

So, here is my encoding question.

I have my service setup and stream. Everything works fine, but only works once. I need him to go in cycles and keep checking. Once this is done by run (), how can I tell it to run () again?

public class NotifyService extends Service{ private long mDoTask; NoteThread notethread; @Override public IBinder onBind(Intent arg0) { return null; } @Override public void onCreate() { mDoTask = System.currentTimeMillis(); notethread = new NoteThread(); notethread.start(); } public class NoteThread extends Thread { NotificationManager nManager; Notification myNote; @Override public synchronized void start() { super.start(); //init some stuff } @Override public void run() { //If it been x time since the last task, do it again //For testing set to every 15 seconds... if(mDoTask + 15000 < System.currentTimeMillis()){ //Take care of business mDoTask = System.currentTimeMillis(); } } } } 
+4
source share
2 answers

In Android docs:

A service is an application component representing either an application's desire to perform longer without interacting with the user or to provide functionality for other applications. Each class of service must have a corresponding declaration in its AndroidManifest.xml package. Services can be started using Context.startService () and Context.bindService ().

Please note that services, like other application objects, are started mainly by the flow of their hosting process. This means that if your service is going to do any intensive processor (for example, MP3 playback) or locks (for example, networks), it must create its own thread to do that work. Further information on this can be found in the Processes and Threads section. The IntentService class is available as a standard implementation of the Service, which has its own thread, where it plans to do its job.

You can find a detailed discussion on how to create services in the Services Document.

In other words, the service does not start in the background unless you put it in a thread. If you put a service that never ends in your application without manually entering the service, then it will be blocked.

Android provides an API for performing background tasks for you, without forcing yourself to call Java threads; It was called AsyncTask , and this is one of the few good design decisions that the Android team has made.

EDIT I forgot to answer your multithreading question. You do not want the thread to execute its run() method more than once. Create an instance of a new thread or place a while around the contents of the run logic that you would like to repeat.

+2
source

For a better understanding of threads, read Brian Goetz's Java Concurrency In Practice.

To better understand the services, I think you should write them as single-threaded and let the Java EE container deploy them to handle threading issues. A federated servlet is a better solution than using your code to manage flows for users.

0
source

All Articles