Android ejecutar hilo en el servicio cada X segundos
Quiero crear un hilo en un servicio de Android que se ejecute cada X segundos
Estoy utilizando actualmente, pero el método postdelayed parece realmente lag mi aplicación.
- Temporizador () como Daemon Vs non Daemon
- ¿Cómo implementar una pantalla de bienvenida con una barra de progreso? androide
- No recibe datos de ubicación del hilo
- Android: cómo utilizar un temporizador
- ¿Cómo puedo cancelar desde Device.StartTimer?
@Override public int onStartCommand(Intent intent, int flags, int startId){ super.onStartCommand(intent, flags, startId); startRepeatingTask(); return startId; } private final static int INTERVAL = 20000; //20 milliseconds Handler m_handler = new Handler(); Runnable m_handlerTask = new Runnable() { @Override public void run() { // this is bad m_handler.postDelayed(m_handlerTask, INTERVAL); } }; void startRepeatingTask() { m_handlerTask.run(); } void stopRepeatingTask() { m_handler.removeCallbacks(m_handlerTask); stopSelf(); }
Quiero hacer un nuevo hilo como este:
public void threadRun() { Thread triggerService = new Thread(new Runnable(){ public void run(){ Looper.prepare(); try{ //do stuff here? }catch(Exception ex){ System.out.println("Exception in triggerService Thread -- "+ex); }//end catch }//end run }, "aThread"); triggerService.start(); //perhaps do stuff here with a timer? timer1=new Timer(); timer1.scheduleAtFixedRate(new methodTODOSTUFF(), 0, INTERVAL); }
No estoy seguro de la mejor manera de hacer un hilo de fondo para ejecutar en un intervalo determinado, la comprensión de apreciación!
- PostAtTime no desencadena
- El temporizador no se detiene en android
- Programación de Android Timer vs. scheduleAtFixedRate
- Equivalente a javax.swing.Timer en Android
- Intentar invocar método virtual
- Android: EditText pierde foco cuando se llama a setText
- ¿Cómo agregar tiempo al temporizador de cuenta atrás?
- ¿Cómo comprobar si un temporizador todavía está funcionando o no?
Hay varias maneras alternativas de hacerlo. Personalmente, prefiero usar ScheduledExecutorService :
ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(5); // This schedule a runnable task every 2 minutes scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() { public void run() { doSomethingUseful(); } }, 0, 2, TimeUnit.MINUTES);
Aquí es cómo ejecutar un hilo de repetición, como lo verá loops cada 1 segundo. No veo retraso con este método.
final Thread t = new Thread(new RepeatingThread()); t.start();
Y la clase:
import android.os.Handler; public class RepeatingThread implements Runnable { private final Handler mHandler = new Handler(); public RepeatingThread() { } @Override public void run() { mHandler.postDelayed(this, 1000); } }
MyService.java
public class MyService extends Service { public static final int notify = 5000; //interval between two services(Here Service run every 5 seconds) int count = 0; //number of times service is display private Handler mHandler = new Handler(); //run on another Thread to avoid crash private Timer mTimer = null; //timer handling @Override public IBinder onBind(Intent intent) { throw new UnsupportedOperationException("Not yet implemented"); } @Override public void onCreate() { if (mTimer != null) // Cancel if already existed mTimer.cancel(); else mTimer = new Timer(); //recreate new mTimer.scheduleAtFixedRate(new TimeDisplay(), 0, notify); //Schedule task } @Override public void onDestroy() { super.onDestroy(); mTimer.cancel(); //For Cancel Timer Toast.makeText(this, "Service is Destroyed", Toast.LENGTH_SHORT).show(); } //class TimeDisplay for handling task class TimeDisplay extends TimerTask { @Override public void run() { // run on another thread mHandler.post(new Runnable() { @Override public void run() { // display toast Toast.makeText(MyService.this, "Service is running", Toast.LENGTH_SHORT).show(); } }); } } }
MainActivity.java
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); startService(new Intent(this, MyService.class)); //start service which is MyService.java } }
Agregue el siguiente código en AndroidManifest.xml
AndroidManifest.xml
<service android:name=".MyService" android:enabled="true" android:exported="true"></service>
Comenzar un runnable
cada pocos segundos va a tener un cierto retraso no importa cómo usted lo corta, ¿no? No veo por qué un Handler
no debe funcionar bien.
Usted podría estar experimentando algunos problemas, porque aunque usted tiene
void startRepeatingTask() { m_handlerTask.run(); }
En su lugar, debe utilizar el controlador y hacer algo como:
void startRepeatingTask() { m_handler.post(m_handlerTask); }
(Como un aparte, la convención en Java es usar el caso del camello, no el caso de la serpiente. Por lo tanto debe ser mHandler, no m_handler, el etc. apenas que le dice porque podría hacer su código más fácil de leer para alguno.
- La compilación de Android en la tienda se comporta de forma diferente a la de un apk instalado a través de USB
- Cómo desplegar una parte de la imagen en android