D
D
Dmitry2015-09-02 10:24:23
Android
Dmitry, 2015-09-02 10:24:23

How to handle button clicks in NotificationManager?

notification_layout.xml

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:id="@+id/linearLayout">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Medium Text"
            android:id="@+id/textView4"
            android:textSize="18dp" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/linearLayout"
        android:layout_alignParentStart="true">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:id="@+id/buttonYes"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Да" />
            <Button
                android:id="@+id/buttonNo"

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Нет" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

RepeatingAlarmService.java
Intent resultYes = new Intent(gcontext, MainActivity.class);
                resultYes.putExtra("YES", 1);
                TaskStackBuilder stackBuilderYes = TaskStackBuilder.create(gcontext);

                stackBuilderYes.addParentStack(MainActivity.class);
                stackBuilderYes.addNextIntent(resultYes);

                PendingIntent resultPendingIntent = stackBuilderYes.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);


                RemoteViews rViews = new RemoteViews(gcontext.getPackageName(), R.layout.notification_layout);
                rViews.setTextViewText(R.id.textView4, DB.getText());
                rViews.setOnClickPendingIntent(R.id.buttonYes, resultPendingIntent);


                manger = (NotificationManager) gcontext.getSystemService(Context.NOTIFICATION_SERVICE);
                Notification notify = new Notification.Builder(gcontext)
                        .setContentTitle("Уведомление")
                        .setContentText(DB.getText())
                        .setTicker("Требуется ответить на вопрос!").setWhen(System.currentTimeMillis() + DB.getServiceRepeatTime()) 
                        .setContentIntent(null)
                        .setDefaults(Notification.DEFAULT_SOUND).setAutoCancel(true)
                        .setSmallIcon(R.drawable.appicon)
                        .setAutoCancel(false)
                        .setContent(rViews)
                        .build();

Pressing one button is processed normally .. question:
1. How to remove the flickering activity? in MainActivity
....
  YES = extras.getInt("YES");
...
       if (YES == 1) {
            Log.i("","YES found");
            database.setYes();
            finish();
        }

Maybe you can call database.setYes(); somehow differently?
2. The notification that appears is not hidden after pressing the button
3. How to hang the event handling on the second button .. did it by analogy with the first .. did not work
TaskStackBuilder stackBuilderYes = TaskStackBuilder.create(gcontext);
                stackBuilderYes.addParentStack(MainActivity.class);
                stackBuilderYes.addNextIntent(resultYes);

                TaskStackBuilder stackBuilderNo = TaskStackBuilder.create(gcontext);
                stackBuilderNo.addParentStack(MainActivity.class);
                stackBuilderNo.addNextIntent(resultNo);

                PendingIntent resultPendingIntent = stackBuilderYes.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
                PendingIntent resultPendingIntent2 = stackBuilderNo.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
 TaskStackBuilder stackBuilderYes = TaskStackBuilder.create(gcontext);
                stackBuilderYes.addParentStack(MainActivity.class);
                stackBuilderYes.addNextIntent(resultYes);

                TaskStackBuilder stackBuilderNo = TaskStackBuilder.create(gcontext);
                stackBuilderNo.addParentStack(MainActivity.class);
                stackBuilderNo.addNextIntent(resultNo);

                PendingIntent resultPendingIntent = stackBuilderYes.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
                PendingIntent resultPendingIntent2 = stackBuilderNo.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
                rViews.setOnClickPendingIntent(R.id.buttonYes, resultPendingIntent);
                rViews.setOnClickPendingIntent(R.id.buttonNo, resultPendingIntent2);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question