web-dev-qa-db-fra.com

Ajout d'une action de bouton dans une notification personnalisée

J'ai fait custom notification et il y a un bouton, je veux effectuer deux functionalities on notification and button click. Je regarde de nombreux liens mais je n'ai pas trouvé le moyen d'ajouter un écouteur de boutons.

Quelqu'un peut-il m'aider? Voici mon code. Merci beaucoup.

 private void startNotification() {
    Intent intent;
    PendingIntent pIntent;
    RemoteViews remoteViews = new RemoteViews(getPackageName(),
            R.layout.mynotification);

    Context context = getApplicationContext();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
            this).setSmallIcon(R.drawable.ic_launcher).setContent(
            remoteViews);

    if (hasFlash) {
        intent = new Intent(context, FlashLight.class);
        pIntent = PendingIntent.getActivity(context, 1, intent, 0);
    } else {
        intent = new Intent(context, BlankWhiteActivity.class);
        pIntent = PendingIntent.getActivity(context, 1, intent, 0);
    }
    builder.setContentIntent(pIntent);
    NotificationManager mNotificationManager = (NotificationManager)      getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notif = builder.setContentTitle("Flashlight")
            .setContentText("Lighten your world!!!").build();
    mNotificationManager.notify(1, notif);

    remoteViews.setOnClickPendingIntent(R.id.closeOnFlash, pIntent);

}

J'ai passé l'ID du bouton (closeOnFlash) dans setOnClickPendingIntent je ne sais pas pourquoi cela ne fonctionne pas.

Et voici mon xml:

<?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" />
30
Syed Raza Mehdi

Lancer la notification comme:

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 utilisé:

<?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" />

Dans le manifeste sous la balise Application:

<receiver Android:name="FlashLight$switchButtonListener" />
50
Syed Raza Mehdi