I would like to show a notification that displays the progress of an ongoing operation. That works well for me. But at the same time the remote view should contain cancel button to stop the ongoing operation. The usual content intent should still do something else, i.e. not cancel the ongoing operation. It seems though that I can only have one intent.
I have to specify a contentIntent that is launched when clicking on the notification: If I don't specify that I get something along those lines:
E/ActivityManager( 62): Activity Manager Crash
E/ActivityManager( 62): java.lang.IllegalArgumentException: contentIntent required ...
For the "cancel" button I set another intent:
Intent cancelSyncIntent = new Intent("com.xyz.CANCEL_SYNC");
contentView.setOnClickPendingIntent(R.id.cancel_sync,
PendingIntent.getBroadcast(context, 0,
cancelSyncIntent, 0));
But this never works. I always get the content intent when the button is clicked. It looks like I cannot use buttons in remote views of notifications?!
I could probably display a text: "<< Press to cancel operation >>", but that seems rather heavy handed.
Update: Afer J.'s recommendations:
final Notification n = new Notification(R.drawable.gen_auto_notification_icon, context.getResources()
.getString(
fastSyncOnly ? R.string.fast_synchronization_running_notification_title
: R.string.synchronization_running_notification_title), new Date().getTime());
n.flags = Notification.FLAG_ONGOING_EVENT;
final RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.in_progress_notification);
n.contentView = contentView;
// Intent cancelSyncIntent = new Intent("com.newsrob.CANCEL_SYNC");
Intent cancelSyncIntent = new Intent();
cancelSyncIntent.setClass(context, FireReceiver.class);
PendingIntent pendingCancelSyncIntent = PendingIntent.getActivity(context, 0, cancelSyncIntent, 0);
contentView.setOnClickPendingIntent(R.id.cancel_sync, pendingCancelSyncIntent);
Intent showDashboardIntent = new Intent(context, DashboardListActivity.class);
PendingIntent showDashboardPendingIntent = PendingIntent.getActivity(context, 0, showDashboardIntent, 0);
n.contentIntent = showDashboardPendingIntent;