Is it possible to add onclick listeners to remoteviews in android
Asked Answered
D

1

6

I have created a notification method as shown below:

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notification;

        notification = new Notification(R.drawable.messageicon, "You have a new message",
                System.currentTimeMillis());
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);
        view.setImageViewResource(R.id.image, R.drawable.ic_launcher);
        view.setTextViewText(R.id.title, "New Message");
        view.setTextViewText(R.id.text, message);
        notification.contentView = view;
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent activity = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        notification.contentIntent = activity;
        notificationManager.notify(0, notification);

I want to add a button on status bar and on click of the button a pop-up should be displayed. Any help would be highly appreciated.

Dacy answered 5/9, 2012 at 15:4 Comment(1)
see developer.android.com/reference/android/widget/RemoteViews.html method is setOnClickPendingIntentDeflower
C
11

You can't do directly, but you can use RemoteViews.setOnClickPendingIntent(int viewId, PendingIntent pendingIntent).

If you want to call the same Activity on all buttons onClick events, make sure you add a Intent.setAction(String action) with a different string so the system do not merge all intents in only one for all buttons.

Then theme your Activity as a Dialog and you'll have a popup.

Cithara answered 22/10, 2012 at 12:21 Comment(1)
Thank you for your explanation, mainly part about Intent.setAction(String action) helped me!Princedom

© 2022 - 2024 — McMap. All rights reserved.