Adding button action in custom notification
Start notification as:
private void startNotification(){
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager =
(NotificationManager) getSystemService(ns);
Notification notification = new Notification(R.drawable.ic_launcher, null,
System.currentTimeMillis());
RemoteViews notificationView = new RemoteViews(getPackageName(),
R.layout.mynotification);
//the intent that is started when the notification is clicked (works)
Intent notificationIntent = new Intent(this, FlashLight.class);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.contentView = notificationView;
notification.contentIntent = pendingNotificationIntent;
notification.flags |= Notification.FLAG_NO_CLEAR;
//this is the intent that is supposed to be called when the
//button is clicked
Intent switchIntent = new Intent(this, switchButtonListener.class);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0,
switchIntent, 0);
notificationView.setOnClickPendingIntent(R.id.closeOnFlash,
pendingSwitchIntent);
notificationManager.notify(1, notification);
}
public static class switchButtonListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Here", "I am here");
FlashOnOff flashLight;
flashLight = new FlashOnOff();
flashLight.flashLightOff();
flashLight.releaseCamera();
}
}
xml used:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="100" >
<ImageView
android:id="@+id/notifiation_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="30"
android:contentDescription="@string/appImage"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/appName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:text="@string/flashLightOn"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/closeOnFlash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="@string/close" />
In manifest under Application tag:
<receiver android:name="FlashLight$switchButtonListener" />
Android: programmatically adding buttons to notification
Read through these questions:
How to add button to notifications in android?
Adding button action in custom notification
Handling buttons inside android notifications
Also take look at the Notification Actions developers guide.
Also it looks like after a notification is created you cannot add actions, so you should create new notification with specified actions, and then replace previous one (assign id to your notifications).
Custom notification with two action button
I had a similar error in my App. The problem was that RemoteViews does not support every kind of layout or view.
In your case, I think the problem is the <View/>
part in your custom_notification.xml.
Try removing or replacing it with a supported layout element. You can find them here: http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
Event OnClick for a button in a custom notification
I am writing code in my MyActivity.java
class that extends android.app.Activity
It creates a custom notification, when user click on the button it sends a broadcast
.
There is a broadcast receiver that receives the broadcast
.
private void createDownloadNotification() {
Intent closeButton = new Intent("Download_Cancelled");
closeButton.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0, closeButton, 0);
RemoteViews notificationView = new RemoteViews(getPackageName(), R.layout.widget_update_notification);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher).setTicker("Ticker Text").setContent(notificationView);
notificationView.setProgressBar(R.id.pb_progress, 100, 12, false);
notificationView.setOnClickPendingIntent(R.id.btn_close, pendingSwitchIntent);
notificationManager.notify(1, builder.build());
}
public static class DownloadCancelReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("Received Cancelled Event");
}
}
Register receiver in AndroidManifest.xml
<receiver android:name=".MainActivity$DownloadCancelReceiver" android:exported="false">
<intent-filter>
<action android:name="Download_Cancelled" />
</intent-filter>
</receiver>
Since it is inner class so have to use $
sign
Widget xml is here
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/btn_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close Me" />
<ProgressBar
android:id="@+id/pb_progress"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Related Topics
Show an Alert Dialog in Broadcast Receiver After a System Reboot
Detecting Whether a Headset Is Plugged into an Android Device or Not
How to Detect When Phone Is Answered or Rejected
What Proguard Configuration Do I Need for Firebase on Android
Adt Will Not Allow Creation of Android Activity
Create Custom Notification, Android
Android Push Notification (Gcm), Is There Any Daily Limit
Java.Lang.Classnotfoundexception on Working App
Is There Any Reason to Use the Support.V4 Library in Android
How to Maintain Multi Layers of Imageviews and Keep Their Aspect Ratio Based on the Largest One
Java.Lang.Illegalargumentexception: Contains a Path Separator
How to Pass Integer from One Activity to Another
Manifest Merger Fails for Appcomponentfactory
Cannot Load Library: Reloc_Library[1285]: Cannot Locate 'Rand'
Recognizing Multiple Keywords Using Pocketsphinx
Error: Fix the Version Conflict (Google-Services Plugin)
Blue Dot and Circle Is Not Shown on Mylocation Using Android Fused Location API