wake-up-neo.com

Hinzufügen einer Schaltflächenaktion in einer benutzerdefinierten Benachrichtigung

Ich habe custom notification Gemacht und es gibt einen Knopf darin, ich möchte zwei verschiedene functionalities on notification and button click Ausführen. Ich habe viele Links angeschaut, aber keinen Weg gefunden, einen Button-Listener hinzuzufügen.

Kann jemand helfen. Hier ist mein Code. Danke vielmals.

 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);

}

Ich habe die Schaltflächen-ID (closeOnFlash) in setOnClickPendingIntent übergeben und weiß nicht, warum sie nicht funktioniert.

Und hier ist mein 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

Benachrichtigung starten als:

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 verwendet:

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

Im Manifest unter Anwendungs-Tag:

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