在提醒应用中向通知按钮添加操作

时间:2018-10-06 20:10:42

标签: java android alarmmanager

在我的程序中,用户收到他创建的任务的通知,并且在通知中有一个按钮,可将用户编写的任务转换为语音。

这意味着我想在通知按钮上添加一个操作,以将用户书面提醒转换为语音。

ReminderalramService.java:

public class ReminderAlarmService extends IntentService {
private static final String TAG = ReminderAlarmService.class.getSimpleName();

private static final int NOTIFICATION_ID = 42;
//This is a deep link intent, and needs the task stack
public static PendingIntent getReminderPendingIntent(Context context, Uri uri) {
    Intent action = new Intent(context, ReminderAlarmService.class);
    action.setData(uri);
    return PendingIntent.getService(context, 0, action, PendingIntent.FLAG_UPDATE_CURRENT);
}

public ReminderAlarmService() {
    super(TAG);
}

@Override
protected void onHandleIntent(Intent intent) {

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Uri uri = intent.getData();

    //Display a notification to view the task details
    Intent action = new Intent(this, AddReminderActivity.class);
    action.setData(uri);
    PendingIntent operation = TaskStackBuilder.create(this)
            .addNextIntentWithParentStack(action)
            .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    //Grab the task description
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);

    String description = "";
    try {
        if (cursor != null && cursor.moveToFirst()) {
            description = AlarmReminderContract.getColumnString(cursor, AlarmReminderContract.AlarmReminderEntry.KEY_TITLE);
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    Notification note = new NotificationCompat.Builder(this)
            .setContentTitle(getString(R.string.reminder_title))
            .setContentText(description)
            .setSmallIcon(R.drawable.ic_add_alert_black_24dp)
            .setContentIntent(operation)
            .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setAutoCancel(true)
            .build();

    manager.notify(NOTIFICATION_ID, note);
}
}

我想在语音通知中添加一个动作。

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

// Create an intent
Intent snoozeIntent = new Intent(this, MyBroadcastReceiver.class);
snoozeIntent.setAction(ACTION_SNOOZE);
snoozeIntent.putExtra(EXTRA_NOTIFICATION_ID, 0);

// Create the action pending intent
PendingIntent snoozePendingIntent =
        PendingIntent.getBroadcast(this, 0, snoozeIntent, 0);

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setContentIntent(pendingIntent)

// add the action to your build like this
        .addAction(R.drawable.ic_snooze, getString(R.string.snooze),
                snoozePendingIntent);