在自定义通知中添加按钮操作
Posted
技术标签:
【中文标题】在自定义通知中添加按钮操作【英文标题】:Adding button action in custom notification 【发布时间】:2014-03-22 10:50:17 【问题描述】:我制作了custom notification
,其中有一个按钮,我想执行两个不同的functionalities on notification and button click
。我查看了许多链接,但找不到添加按钮侦听器的方法。
谁能帮忙。这是我的代码。非常感谢。
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_
android:layout_
android:gravity="center"
android:orientation="horizontal"
android:weightSum="100" >
<ImageView
android:id="@+id/notifiation_image"
android:layout_
android:layout_
android:layout_weight="30"
android:contentDescription="@string/appImage"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/appName"
android:layout_
android:layout_
android:layout_weight="50"
android:gravity="center"
android:text="@string/flashLightOn"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/closeOnFlash"
android:layout_
android:layout_
android:layout_weight="20"
android:text="@string/close" />
【问题讨论】:
这个帖子可能有帮助吗? ***.com/questions/5479165/… 这个帖子应该回答你想要做什么:***.com/questions/12438209/… 好的,非常感谢,我会看看。 实际上第二个对我的 HTC 感觉非常有效,还没有检查兼容性问题。 【参考方案1】:开始通知为:
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_
android:layout_
android:gravity="center"
android:orientation="horizontal"
android:weightSum="100" >
<ImageView
android:id="@+id/notifiation_image"
android:layout_
android:layout_
android:layout_weight="30"
android:contentDescription="@string/appImage"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/appName"
android:layout_
android:layout_
android:layout_weight="50"
android:gravity="center"
android:text="@string/flashLightOn"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/closeOnFlash"
android:layout_
android:layout_
android:layout_weight="20"
android:text="@string/close" />
在应用标签下的清单中:
<receiver android:name="FlashLight$switchButtonListener" />
【讨论】:
@PoojaShah 乐于提供帮助 :) Manifest 中的声明是如何工作的? FlashLight 您的课程是否包含 swiftButtonListener ?是应用名称吗? 这真是个好主意!感谢分享! @ShaikMDAshiq 是的,它是包含接收器的类,您也可以使用类的完全分类名称 这是否被视为必须在 Manifest 中声明的全局广播?如果不是,为什么不能用作LocalBroadcast?以上是关于在自定义通知中添加按钮操作的主要内容,如果未能解决你的问题,请参考以下文章