A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© linilq 初级黑马   /  2014-4-4 09:01  /  1564 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

先上两张图:
第一张:收缩时的notification  第二张:扩展开的notification
     

请指教一下着两种形式的notification具体实现方法,能上代码最好。
楼主自己还很菜,不知道自己的思路对不对
1、收缩时的notification直接由remoteview定义(imageview+TextView+imageButton),然后给四个按键设置监听器;
2、第二个几乎没有思路,只是想到addAction添加三个功能键,在点击时设置相应的改变(播放->暂停);但是整体是怎么设计的
,特别是在扩展的情况下它的关闭按键、左边的大图片是怎么设置的不懂  请大家指教!谢谢!

评分

参与人数 1技术分 +1 收起 理由
zzkang0206 + 1

查看全部评分

1 个回复

倒序浏览
使用自定义的Notification
要创建一个自定义的Notification,可以使用RemoteViews。要定义自己的扩展消息,首先要初始化一个RemoteViews对象,然后将它传递给Notification的contentView字段,再把PendingIntent传递给contentIntent字段。以下示例代码是完整步骤:

//1、创建一个自定义的消息布局 view.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent" android:layout_height="fill_parent">

<ImageView android:id="@+id/image" android:layout_width="wrap_content"

android:layout_height="fill_parent" android:layout_marginRight="10dp" />

<TextView android:id="@+id/text" android:layout_width="wrap_content"

android:layout_height="fill_parent" android:textColor="#000" />

</LinearLayout>

//2、在程序代码中使用RemoteViews的方法来定义image和text。然后把RemoteViews对象传到contentView字段

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

contentView.setImageViewResource(R.id.image,R.drawable.icon);

contentView.setTextViewText(R.id.text,”Hello,this message is in a custom expanded view”);

notification.contentView = contentView;

//3、为Notification的contentIntent字段定义一个Intent(注意,使用自定义View不需要setLatestEventInfo()方法)

Intent notificationIntent = new Intent(this,Main.class);

PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);

notification.contentIntent = contentIntent;

//4、发送通知

mNotificationManager.notify(2,notification);

//以下是全部示例代码

//创建一个NotificationManager的引用

String ns = Context.NOTIFICATION_SERVICE;

NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);

//定义Notification的各种属性

int icon = R.drawable.icon; //通知图标

CharSequence tickerText = "Hello"; //状态栏显示的通知文本提示

long when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示

//用上面的属性初始化Nofification

Notification notification = new Notification(icon,tickerText,when);

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

contentView.setImageViewResource(R.id.image, R.drawable.iconempty);

contentView.setTextViewText(R.id.text, "Hello,this is JC");

notification.contentView = contentView;

Intent notificationIntent = new Intent(this,Main.class);

PendingIntent contentIntent = PendingIntent.getActivity(this,0,notificationIntent,0);

notification.contentIntent = contentIntent;

//把Notification传递给NotificationManager

mNotificationManager.notify(0,notification);

评分

参与人数 1技术分 +1 收起 理由
zzkang0206 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马