开发者

Implementing an interactive notification in the status bar (Android)

I'd like to know how to disable the highlighting effect when the user clicks the notification in the status bar and furthermore, I'd like to allow the user to interact directly with the RemoteView I have placed in the notification by means of button pressing.

I know this can be done开发者_如何转开发 as HTC's Sense has an ongoing notification while a call is in progress that has accomplished the above goals.

Please let me know if you have any ideas, specifically, how do I set an OnClickListener for a view nested within my RemoteView?


I have not tried this but it should work the same way that widgets do using RemoteViews.setOnClickPendingIntent. One way to disable the row's selection highlight might be to add a click Intent to the outside layout element and then don't do anything with it.

For example if your custom layout looks like this.

<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>

Add a do_nothing intent to the layout_root,

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);

BTW, HTC has the ability to modify android in ways that a normal developer can't, so it's not always good to use their stuff as an example of what is possible.


I am still working on making some similar code work, but my observation so far is that different versions of Android handle things differently. on Ice Cream Sandwich, using setOnClickPendingIntent on individual items in the layout works. However, on earlier versions of Android, the contentIntent for the notification fires first, and the intent for the button never fires. There may be some way around this, but I have yet to find it. Giving a do-nothing intent for the notification's contentintent doesn't seem to help, as it still grabs the click.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜