在自定义通知中添加按钮操作

39

我制作了一个自定义通知,并且其中有一个按钮,我想在通知和按钮点击时执行两种不同的功能。我查看了很多链接,但找不到添加按钮监听器的方法。

有人能帮忙吗?这是我的代码。非常感谢。

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

}

我已经在setOnClickPendingIntent中传递了按钮ID (closeOnFlash),但不知道为什么它不起作用。

这是我的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" />

2
也许这个线程可以帮助?https://dev59.com/-m035IYBdhLWcg3wVuh1 - user1406716
1
这个线程应该可以回答你想做的事情:https://dev59.com/QGcs5IYBdhLWcg3w-YpD - user1406716
好的,非常感谢,我会去看一下的。 - Syed Raza Mehdi
实际上,第二个方法在我的HTC Sensation上真的有效,我还没有检查兼容性问题。 - Syed Raza Mehdi
1个回答

66

开始通知:

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:

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

在应用程序标签下的清单文件中:

<receiver android:name="FlashLight$switchButtonListener" />

1
清单文件中的声明是如何工作的?FlashLight是包含swiftButtonListener的类吗?它是应用程序名称吗? - Bullionist
这真是一个很好的想法!谢谢分享! - Mohammed AlBanna
@ShaikMDAshiq 是的,它是包含接收器的类,您还可以使用完全限定的类名称。 - Syed Raza Mehdi
这被认为是一个全局广播吗?它必须在清单中声明吗?如果不是,为什么不能将其用作本地广播? - IgorGanapolsky
如何添加另一个按钮点击: - rams
显示剩余10条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接