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

标签 android android-layout android-intent android-fragments android-notifications

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

谁能帮忙。这是我的代码。非常感谢。

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

最佳答案

开始通知为:

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

在应用标签下的 list 中:

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

关于android - 在自定义通知中添加按钮操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21925688/

相关文章:

android - Android平台版本历史分布

Android官方闹钟星期样式

Android:AppWidgetProvider 无法更改更新间隔时间

Android图像编辑器 Intent : retrieving saved file location

android - 如何停止 BarcodeDetector

android - 无法通过 IMPLICIT Intent 启动 Intent 服务

android - 我可以将外部 XML 文件用于 Android 中的布局吗?

android - 什么Layout最好根据坐标来设置它的inner views

java - 自定义硬件按钮事件/Intent

Android Studio 3.1 - 插件 Android 支持异常