Добавление действия к кнопке уведомления в приложении напоминания - PullRequest
0 голосов
/ 06 октября 2018

В моей программе пользователь получает уведомление о задаче, которую он создал, а в уведомлениях есть кнопка, которая преобразует написанные пользователем задачи в речь.

Это означает, что я хочу добавитьдействие для кнопки уведомления, которая преобразует письменные напоминания пользователя в речь.

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 голосов
/ 06 октября 2018

Вы можете выполнить действие, подобное этому:

// 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);
...