usando tutorial studio programar manager ejemplo cancel app alarma android service alarmmanager

android - tutorial - AlarmManager o Servicio



tutorial alarmmanager (1)

Soy desarrollador de C ++ y estoy desarrollando mi primera aplicación de Android. Mi aplicación es un tipo especial de Recordatorio. Estoy buscando la mejor manera de hacerlo. He intentado esto enfoques:

  1. Use un servicio
  2. Use un AlarmManager

Mi pregunta es si puedo usar AlarmManager solo? ¿Es una tarea que consume mucho tiempo de la CPU teniendo en cuenta que mi AlarmManager debe dispararse cada 1 segundo? (Parece que cada vez que se ejecuta un AlarmManager, se crea un nuevo proceso, excepto el proceso principal, que se elimina inmediatamente).

Si uso un servicio, entonces mi aplicación siempre debe permanecer en la memoria y también ¿qué ocurre si el usuario la mata?

¿Cómo funciona Android Alarms (aplicación predeterminada instalada)?

Cualquier ayuda sería apreciada.


Utiliza un servicio que devuelve START_STICKY y haz que comience desde el principio, de esta forma tu aplicación se ejecutará todo el tiempo, incluso si el sistema la mata por recursos después de un tiempo estará funcionando nuevamente normalmente, y el usuario lo matará, esto es algo incluso las aplicaciones grandes se quejan de Whatsapp como ves en los desgastados al instalar WhatsApp por primera vez. aquí hay un ejemplo de cómo debe ser el servicio:

public class Yourservice extends Service{ @Override public void onCreate() { super.onCreate(); // Oncreat called one time and used for general declarations like registering a broadcast receiver } @Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); // here to show that your service is running foreground mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Intent bIntent = new Intent(this, Main.class); PendingIntent pbIntent = PendingIntent.getActivity(this, 0 , bIntent, Intent.FLAG_ACTIVITY_CLEAR_TOP); NotificationCompat.Builder bBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("title") .setContentText("sub title") .setAutoCancel(true) .setOngoing(true) .setContentIntent(pbIntent); barNotif = bBuilder.build(); this.startForeground(1, barNotif); // here the body of your service where you can arrange your reminders and send alerts return START_STICKY; } @Override public IBinder onBind(Intent intent) { return null; } @Override public void onDestroy() { super.onDestroy(); stopForeground(true); } }

esta es la mejor receta para el servicio continuo para ejecutar sus códigos.