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

4
我想知道如何禁用当用户点击状态栏中的通知时高亮效果,此外,我想让用户通过按钮按压直接与我放置在通知中的RemoteView进行交互。我知道这可以通过HTC Sense实现,在进行呼叫时会有一个正在进行的通知,已经实现了上述目标。请让我知道您是否有任何想法,具体而言,如何为嵌套在我的RemoteView中的视图设置OnClickListener?
2个回答

2
我还没有尝试过,但是使用RemoteViews.setOnClickPendingIntent应该与小部件的方式相同。禁用行选择高亮的一种方法可能是向外部布局元素添加点击意图,然后不执行任何操作。
例如,如果您的自定义布局如下所示。
<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 有能力以普通开发者无法做到的方式修改安卓系统,因此并不总是好的示范。


0

我仍在努力让一些类似的代码工作,但我目前的观察是不同版本的Android处理方式不同。在Ice Cream Sandwich上,在布局中为各个项使用setOnClickPendingIntent可以正常工作。然而,在早期版本的Android上,通知的contentIntent会首先触发,而按钮的意图永远不会触发。可能有某种方法可以解决这个问题,但我还没有找到。为通知的contentintent提供一个无用的意图似乎没有帮助,因为它仍然会捕获点击。


消息提醒- HoneyComb设置了分界线。从HoneyComb开始,它们就能够正常工作了。 - Falcon

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