Alarm Manager响应通知

时间:2017-07-16 11:37:45

标签: android broadcastreceiver alarmmanager android-notifications

我有一个警报设置为每天触发,我的问题是,我将在通知中有两个按钮,推迟按钮,我得到它按钮。

我是怎么做的

设置闹钟的代码:

Calendar calendar = Calendar.getInstance();


            calendar.set(Calendar.HOUR_OF_DAY, 17);
            calendar.set(Calendar.MINUTE, 38);
            calendar.set(Calendar.SECOND, 00);


            Intent intent = new Intent(getApplicationContext(), Notification_reciever.class);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);

            AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

收件人中的代码:

Consulta_Tratamento consulta_tratamento = new Consulta_Tratamento();

String remedio = consulta_tratamento.getMedicamento();     String dose = consulta_tratamento.getDosagem();

PendingIntent pTomei = PendingIntent.getActivity(context, 0, new Intent(context, actMarcaCon.class),0);
PendingIntent pAdiar = PendingIntent.getActivity(context, 0, new Intent(context, actMarcaCon.class),0);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle("MHS")
        .setContentText(/*Colocar nome do usuário*/"Esta na Hora de Tomar Seu Remédio:" + remedio +
                " /n dosagem:" + dose)
        .setAutoCancel(true)
        .setSound(defaultSoundUri)
        .setVibrate(new long[]{100, 250, 100, 500})
        .addAction(R.drawable.ic_adiar, "Adiar", pAdiar)
        .addAction(R.drawable.ic_tomei, "Tomei", pTomei)
        .setPriority(Notification.PRIORITY_HIGH);

if(intent!=null) {
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
            PendingIntent.FLAG_ONE_SHOT);
    notificationBuilder.setContentIntent(pendingIntent);
}


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

notificationManager.notify(0, notificationBuilder.build());

我的问题是,在add action方法中,我只能传递和pendingIntent,而不是方法,那么如何让我的按钮调用方法而不是意图呢?

1 个答案:

答案 0 :(得分:0)

  

如何让我的按钮调用方法而不是意图?

你不能。

在用于创建Intent的{​​{1}}中设置“额外”。在PendingIntent和/或onCreate()中检查onNewIntent()中是否存在“额外”,并将其用作调用该方法的触发器。