在状态栏中实现交互式通知(Android)

我想知道当用户点击状态栏中的通知时如何禁用突出显示效果,此外,我想允许用户通过按下按钮直接与我在通知中放置的RemoteView进行交互.

我知道这可以做到,因为HTC的Sense持续通知,而正在进行的呼叫已经完成了上述目标.

如果您有任何想法,请告诉我,具体来说,如何为嵌入在RemoteView中的视图设置OnClickListener?

最佳答案 我没有尝试过这个,但它的工作方式与使用RemoteViews.setOnClickPendingIntent的小部件相同.禁用行选择突出显示的一种方法可能是向外部布局元素添加单击Intent,然后不对其执行任何操作.

例如,如果您的自定义布局如下所示.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/layout_root"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    <ImageView android:id="@+id/button"
           android:layout_width="wrap_content"
           android:layout_height="fill_parent"
           android:src="@drawable/button"/>
    <TextView android:id="@+id/text"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:text="Some Text"  />
</LinearLayout>

向layout_root添加do_nothing intent,

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.yourlayout);
notification.contentView = contentView;

PendingIntent peClick = PendingIntent.getBroadcast(this, 0, new Intent("com.BUTTON_CLICK"), 0);
contentView.setOnClickPendingIntent(R.id.button, peClick);
PendingIntent peNothing = PendingIntent.getBroadcast(this, 0, new Intent("com.DO_NOTHING"), 0);
contentView.setOnClickPendingIntent(R.id.layout_root, peNothing);

顺便说一句,HTC能够以普通开发人员无法修改的方式修改android,因此使用他们的东西作为可能的例子并不总是好的.

点赞