java - AlarmManager 重复只触发一次

标签 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);
    }
}

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

提前致谢。

最佳答案

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

然后在onReceive()中,调用setAlarm()方法设置下一个闹钟

关于java - AlarmManager 重复只触发一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56756517/

相关文章:

android - 滚动时显示随机项目的getView

java - Eclipse 运行配置机器名称

java - 在更少的内存中计算唯一的字符串

java - 在 Java 服务器中发送 edittext 时出错。查看代码

android - 适用于手机和平板电脑的不同 APK

java - Android:倒数计时器,例如10:00 到 00:00?使用 OnclickListener 到 TextView?

java - 在 spring boot 中添加外部静态文件(css、js、png ...)

java - GCP-PUBSUB :-sun. security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径

java - 如何在 Android 编程中从图像中选择像素并降低该像素的不透明度

java - 是否可以从 Kotlin 注释链接 Java 方法?