En mi aplicación, estoy tratando de iniciar un servicio usando el administrador de alarmas. Cuando estoy haciendo clic en un botón, el servicio debe comenzar en un momento particular que estoy dando. Mi código para el Administrador de alarma se da a continuación:¿Cómo iniciar el servicio usando Alarm Manager en Android?
public void onClick(View view)
{
if(view == m_btnActivate)
{
Calendar cur_cal = Calendar.getInstance();
cur_cal.setTimeInMillis(System.currentTimeMillis());
cur_cal.add(Calendar.SECOND, 50);
Log.d("Testing", "Calender Set time:"+cur_cal.getTime());
Intent intent = new Intent(DashboardScreen.this, ServiceClass.class);
Log.d("Testing", "Intent created");
PendingIntent pi = PendingIntent.getService(DashboardScreen.this, 0, intent, 0);
AlarmManager alarm_manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm_manager.set(AlarmManager.RTC, cur_cal.getTimeInMillis(), pi);
Log.d("Testing", "alarm manager set");
Toast.makeText(this, "ServiceClass.onCreate()", Toast.LENGTH_LONG).show();
}
}
Y abajo es mi clase de servicio:
public class ServiceClass extends Service{
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.d("Testing", "Service got created");
Toast.makeText(this, "ServiceClass.onCreate()", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Toast.makeText(this, "ServiceClass.onStart()", Toast.LENGTH_LONG).show();
Log.d("Testing", "Service got started");
}
}
Si el servicio se iniciará debe imprimir los mensajes de registro en la clase de servicio. Pero no está mostrando. ¿Alguien puede ayudar?
Mencioné el nombre, pero no Android: enabled = "true" –
Use alarmManager.cancel (pintent); para cancelar el administrador de alarmas – user370305
'Android: habilitado' de forma predeterminada es 'verdadero' – Tautvydas