AlarmManager仅重复触发一次

时间:2019-06-25 14:36:33

标签: java android

我已在我的应用中设置了一个警报,每隔X分钟触发一次通知。第一次有效,但随后不再重复。

我正在使用的API是API 18(Android 4.3)。

MainActivity.class

public void setAlarm(Consumo con){
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        int id = (int) con.getId();
        calendar.add(Calendar.MINUTE, con.getTime());
        Intent notification = new Intent(MainActivity.this, AlarmReceiver.class);
        notification.putExtra("cadena", con.getName()+
                " "+con.getQuantity()+" "+ con.getType()+" "+con.getPills()+" comprimidos");
        notification.putExtra("id", id);
        PendingIntent pendingNotif = PendingIntent.getBroadcast(MainActivity.this,
                id, notification, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), calendar.getTimeInMillis(), pendingNotif);
    }

AlarmReceiver.class

public class AlarmReceiver extends BroadcastReceiver {
    private static String CHANNEL_ID = "medicinas.android.unex.cum.es.app";

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent notificationIntent = new Intent(context, NotificationActivity.class);
        Bundle b = intent.getExtras();
        int id = b.getInt("id");
        notificationIntent.putExtra("id", id);
        TaskStackBuilder tsb = TaskStackBuilder.create(context);
        tsb.addParentStack(NotificationActivity.class);
        tsb.addNextIntent(notificationIntent);

        Intent tomada = new Intent(context, NotificationActivity.class);
        tomada.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        tomada.putExtra("id", id);
        PendingIntent pTomada = PendingIntent.getActivity(context, 155, tomada, PendingIntent.FLAG_ONE_SHOT);

        PendingIntent pendingIntent = tsb.getPendingIntent(id, PendingIntent.FLAG_UPDATE_CURRENT);


        Notification.Builder builder = new Notification.Builder(context);
        Notification notification = builder.setContentTitle("Control de Medicinas")
                .setContentText(intent.getExtras().getString("cadena"))
                .setTicker("¡Hora de tomar la medicina")
                .setSmallIcon(R.mipmap.ic_launcher)
                .addAction(android.R.drawable.btn_default, "Medicina tomada", pTomada)
                .setContentIntent(pendingIntent).build();

        notification.defaults = Notification.DEFAULT_SOUND;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(CHANNEL_ID);
        }

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID,
                    "MedicinaNotificacion",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(id, notification);


    }
}

接收修改后的邮件

public class AlarmReceiver extends BroadcastReceiver {
    private static String CHANNEL_ID = "medicinas.android.unex.cum.es.app";
    private Context mContext;

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent notificationIntent = new Intent(context, NotificationActivity.class);
        Bundle b = intent.getExtras();
        mContext = context;
        int id = b.getInt("id");
        notificationIntent.putExtra("id", id);
        Consumo con = new Consumo(id, b.getString("name"), b.getInt("quantity"),
                b.getString("type"), b.getInt("pills"), true, b.getInt("time"));
        TaskStackBuilder tsb = TaskStackBuilder.create(context);
        tsb.addParentStack(NotificationActivity.class);
        tsb.addNextIntent(notificationIntent);

        Intent tomada = new Intent(context, NotificationActivity.class);
        tomada.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
        tomada.putExtra("id", id);
        String cadena = b.getString("name")+
                " "+b.getInt("quantity")+" "+
                b.getString("type")+" "+b.getInt("pills")+" comprimidos";
        PendingIntent pTomada = PendingIntent.getActivity(context, 155, tomada, PendingIntent.FLAG_ONE_SHOT);

        PendingIntent pendingIntent = tsb.getPendingIntent(id, PendingIntent.FLAG_UPDATE_CURRENT);


        Notification.Builder builder = new Notification.Builder(context);
        Notification notification = builder.setContentTitle("Control de Medicinas")
                .setContentText(cadena)
                .setTicker("¡Hora de tomar la medicina")
                .setSmallIcon(R.mipmap.ic_launcher)
                .addAction(android.R.drawable.btn_default, "Medicina tomada", pTomada)
                .setContentIntent(pendingIntent).build();

        notification.defaults = Notification.DEFAULT_SOUND;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(CHANNEL_ID);
        }

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                    CHANNEL_ID,
                    "MedicinaNotificacion",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
            notificationManager.createNotificationChannel(channel);
        }
        setAlarm(con);
        notificationManager.notify(id, notification);


    }

    public void setAlarm(Consumo con){
        AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();
        int id = (int) con.getId();
        calendar.add(Calendar.MINUTE, con.getTime());
        Intent notification = new Intent(mContext , AlarmReceiver.class);
        notification.putExtra("cadena", con.getName()+
                " "+con.getQuantity()+" "+ con.getType()+" "+con.getPills()+" comprimidos");
        notification.putExtra("id", id);
        notification.putExtra("name", con.getName());
        notification.putExtra("quantity", con.getQuantity());
        notification.putExtra("type", con.getType());
        notification.putExtra("pills", con.getPills());
        notification.putExtra("time", con.getTime());
        Log.i("DEBUG RECEIVER: ", ""+con.getTime());
        PendingIntent pendingNotif = PendingIntent.getBroadcast(mContext,
                id, notification, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager.set(AlarmManager.RTC_WAKEUP, con.getTime()*60*1000, pendingNotif);
    }
}

如果您需要更多代码告诉我,我会尽快添加。

谢谢。

2 个答案:

答案 0 :(得分:1)

尝试: alarmManager.set(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),calendar.getTimeInMillis(),endingNotif);

然后在onReceive()中,调用setAlarm()方法来设置下一个警报

答案 1 :(得分:0)

我认为问题出在这行:

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), calendar.getTimeInMillis(), pendingNotif);

假设您希望每2分钟发出一次警报,应该是:

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 2*60*1000, pendingNotif);

在上面的示例中,将2替换为您希望闹钟响起的分钟数。