Sinisa Sinisa - 1 year ago 162
Java Question

Android notification with RemoteViews - having activity associated with RemoteViews layout

I've been researching on how to create custom-layout notification using RemoteView.
So far, I am able to create notification with contentView and bigContentView pointing to a RemoteView with a custom layout xml. However, what does not happen, is to have activity (associated with custom layout) started, when this RemoteView is created.

I've double checked and in my layout xml, it appears to have correct activity class name:

<RelativeLayout xmlns:android=""
tools:context=".LLMNotificationActivity" >

..... the rest are standard layout items: images, buttons and text


In manifest file, right after main application main activity, notification activity is also added:

<category android:name="android.intent.category.LAUNCHER" />

I would expect when notification uses RemoteViews for it's content, that this RemoteViews will launch activity that is attached to it's layout definition. However it appears not.

Here is how I create a notification in main application activity:

protected void startNoti()
if( noti!=null ) return;

Context context = getApplicationContext();

RemoteViews contentView = new RemoteViews(context.getPackageName(),R.layout.activity_noti1);

Notification.Builder notibuilder = new Notification.Builder(context);
notibuilder.setContentTitle(" ");
notibuilder.setContentText(" ");

manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
noti =;

noti.contentView = contentView;

manager.notify(NOTIFICATION_ID, noti);

LLMNotificationActivity activity class is defined as usual:

public class LLMNotificationActivity extends Activity {
.... etc.... constructor, some button on-click handlers, nothing spectacular...

Can anyone point to me what I am missing or if I have misunderstood what RemoteViews can do? My understanding is that RemoteViews should, once created, invoke activity associated with it's layout. Or - is there some API I've missed that explicitly can set intent of the RemoteViews?

What I have found so far are only setting content Intent which basically just launches an activity once user touches notification. What I'm looking for is to handle touches to some of UI elements inside custom-layout notification, not to launch an activity regardless where user clicks on notification surface.

For example, if I have 3 icons (ImageView) in a RemoteViews which notification uses, I'd like to be able to handle touch on each one of them. I can't imagine this wouldn't be possible as if it's not, what's the point of having RemoteViews in notification :-)

Thanks in advance

Answer Source

You have to associate the activity thought setOnClickPendingIntent to launch the activity from remote view as below...You can set any of the layout id in the remoteview to click.

 Intent intent = new Intent(context, YourActivity.class);
 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,   PendingIntent.FLAG_UPDATE_CURRENT);
 RemoteViews removeWidget = new RemoteViews(context.getPackageName(), R.layout.your_layout);
 removeWidget.setOnClickPendingIntent(, pendingIntent);

provide an +id/layout_id to the relative layout your using.

If you have to launch the activity when user click on the notification, then you have to use PendingIntent as....

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
    Intent notificationIntent = new Intent(this, YourActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(
            this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

For 3 buttons , You have to use create a custom RemoteView and use PendingIntent. some thing as below...

Here is the custom remote view i am using for one of my media player app. it has three button to handler click.

public class RemoveControlWidget extends RemoteViews
private final Context mContext;

public static final String ACTION_PLAY = "";

public static final String ACTION_PREVIOUS = "";

public static final String ACTION_NEXT = "";

public RemoveControlWidget(Context context , String packageName, int layoutId)
    super(packageName, layoutId);
    mContext = context;
    Intent intent = new Intent(ACTION_PLAY);
    PendingIntent pendingIntent = PendingIntent.getService(mContext.getApplicationContext(),100,
    intent = new Intent(ACTION_PREVIOUS);
    pendingIntent = PendingIntent.getService(mContext.getApplicationContext(),101,
    intent = new Intent(ACTION_NEXT);
    pendingIntent = PendingIntent.getService(mContext.getApplicationContext(),102,