【问题标题】:Implementing an interactive notification in the status bar (Android)在状态栏中实现交互式通知(Android)
【发布时间】:2010-06-22 07:20:16
【问题描述】:

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

我知道这是可以做到的,因为 HTC 的 Sense 在完成上述目标的通话过程中会持续发出通知。

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

【问题讨论】:

    标签: android notifications statusbar


    【解决方案1】:

    我没有尝试过,但它的工作方式应该与小部件使用 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,因此使用他们的东西作为可能的例子并不总是好的。

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • fyi- HoneyComb 设置了分界线。从 HoneyComb 开始,它们就起作用了。
      猜你喜欢
      • 2013-12-19
      • 2019-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多