将click事件添加到通知中的按钮

时间:2017-05-25 15:59:07

标签: android notifications

所以我有一个警报应用......当接收器从警报类获取意图时,它会创建一个通知并构建它......但我似乎无法弄清楚如何将onclick事件添加到该按钮。 .i希望它实现一个不只是获得意图的功能

这是我的接收器

 public class AlarmReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context arg0, Intent arg1) {
    Context context= arg0;
    Intent intent = new Intent(context,MainActivity.class);
    PendingIntent pendingIntent=PendingIntent.getActivity(context,0,intent,0);

    NotificationCompat.Builder mBuilder =
            (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.mini)
                    .setContentTitle(context.getResources().getString(R.string.message_box_title))
                    .setContentText(context.getResources().getString(R.string.message_timesheet_not_up_to_date))
                    .addAction(R.drawable.bell,"snooze",pendingIntent);
    Intent resultIntent = new Intent(context, MainActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(MainActivity.class);
    stackBuilder.addNextIntent(resultIntent);
    PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(1, mBuilder.build());







    Toast.makeText(arg0, "Alarm received!", Toast.LENGTH_LONG).show();

    Integer get_your_alarm_choice = arg1.getExtras().getInt("alarm_choice");
    Log.e("alarm choice is",get_your_alarm_choice.toString());
}

任何帮助都会非常感激

1 个答案:

答案 0 :(得分:0)

我认为您想在通知中添加自定义按钮,并希望点击它。

如果您需要,请尝试以下代码:

您必须使用RemoteViews。

我创建了一个名为notification_normal_view.xml的自定义布局。

在我的notification_normal_view中,我有一个TextView ietxtSnooze,点击后我想打开SnoozeActivity,如果我点击通知的任何其他部分,我想打开MainActivity。

所以在你的接收器中:

// Using RemoteViews to bind custom layouts into Notification
RemoteViews notificationView = new RemoteViews(context.getPackageName(), R.layout.notification_normal_view);

Intent snoozeIntent = new Intent(context, SnoozeActivity.class);
snoozeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TASK | Notification.FLAG_AUTO_CANCEL);

PendingIntent pSnoozeIntent = PendingIntent.getBroadcast(context,NOTIFICATION_ID,snoozeIntent,PendingIntent.FLAG_UPDATE_CURRENT);

Intent intent = new Intent(context, ExoVideoPlayer.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_CLEAR_TASK | Notification.FLAG_AUTO_CANCEL);
PendingIntent pIntent = PendingIntent.getActivity(context, NOTIFICATION_ID,intent, PendingIntent.FLAG_UPDATE_CURRENT);

notificationView.setOnClickPendingIntent(R.id.txt_snooze, pSnoozeIntent);

Notification notificationBuilder = new Notification.Builder(context)
                .setSound(soundUri)
                .setSmallIcon(icon)
                .setAutoCancel(true)
                .build();

//set your view to notification
notificationBuilder.contentView = notificationView;
notificationBuilder.flags = Notification.FLAG_AUTO_CANCEL;
notificationBuilder.icon = R.mipmap.ic_launcher;
notificationBuilder.contentIntent = pIntent;

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

mNotificationManager.notify(NOTIFICATION_ID, notificationBuilder);