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

时间:2010-06-22 07:20:17

标签: android notifications statusbar

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

我知道这可以做到,因为HTC的Sense在正在进行的通话中有持续通知,已完成上述目标。

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

2 个答案:

答案 0 :(得分:2)

我没有尝试过这个,但它的工作方式应该与小部件使用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意图,

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,所以使用他们的东西作为可能的例子并不总是好的。

答案 1 :(得分:0)

我仍在努力制作类似的代码,但到目前为止,我的观察是不同版本的Android处理不同的东西。在冰淇淋三明治上,使用setOnClickPendingIntent对布局中的各个项目进行操作。但是,在Android的早期版本中,通知的contentIntent会先触发,而按钮的意图永远不会触发。可能有一些方法,但我还没有找到它。为通知的contentintent提供无操作意图似乎没有帮助,因为它仍然抓住了点击。