Tengo problemas para ejecutar un temporizador en un servicio que he creado. La tarea que llama el temporizador simplemente no se llama. Sé que el servicio comienza cuando he puesto brindis dentro de él y se llaman, pero no cuando están dentro del temporizador. Ayuda apreciada.Android Timer dentro de un servicio
clase de servicio:
public class LocalService extends Service
{
private static Timer timer = new Timer();
private Context ctx;
public IBinder onBind(Intent arg0)
{
return null;
}
public void onCreate()
{
super.onCreate();
ctx = this;
startService();
}
private void startService()
{
timer.scheduleAtFixedRate(new mainTask(), 0, 5000);
}
private class mainTask extends TimerTask
{
public void run()
{
Toast.makeText(ctx, "test", Toast.LENGTH_SHORT).show();
}
}
public void onDestroy()
{
super.onDestroy();
Toast.makeText(this, "Service Stopped ...", Toast.LENGTH_SHORT).show();
}
}
Clase principal:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startService(new Intent(RingerSchedule.this, LocalService.class));
}
Gracias , trabajó. – Woody
Exactamente la respuesta correcta. – Nakedible
estaba buscando esta respuesta por mucho tiempo. – santBart