Answer the question
In order to leave comments, you need to log in
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>
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();
....
YES = extras.getInt("YES");
...
if (YES == 1) {
Log.i("","YES found");
database.setYes();
finish();
}
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 questionAsk a Question
731 491 924 answers to any question