Notification

1. Notification构造器的参数 分别为:在状态条上显示的图标、文字和Notification的当前时间

// Choose a drawable to display as the status bar icon
int icon = R.drawable.icon;
 
// Text to display in the status bar when the notification is launched
String tickerText = “Notification”;
 
// The extended status bar orders notification in time order
// Notification显示的时间, currentTimeMills表示立即显示
long when = System.currentTimeMillis();

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

 

2. setLatestEventInfo()用来配置Notification在扩展的状态窗口中的外观 。扩展的状态窗口将显示图标和在构造函数中传入的时间,以及显示Notification标题和Notification内容。Notification可通过指定PendingIntent来触发。

Context context = getApplicationContext();
 
// Text to display in the extended status window
String expandedText = “Notification Text”;
 
// Title for the expanded status
String expandedTitle = “Notification Title”;
 
// Intent to launch an activity when the extended text is clicked
Intent intent = new Intent(this, MyActivity.class);
 
PendingIntent launchIntent = PendingIntent.getActivity(context, 0, intent, 0);
notification.setLatestEventInfo(context, expandedTitle, expandedText, launchIntent);

// Notification显示时会伴随音乐
// Notification.DEFAULT_VIBRATE:振动;Notification.DEFAULT_ALL:音乐+振动
notification.defaults = Notification.DEFAULT_SOUND;

使用setLastestEventInfo可更新数据集以呈现最新的消息(例如,接收多个SMS消息时更新SMS的数目)。任何对Notification的变更都需要重新触发setLastestEventInfo。振动需要在AndroidManifest.xml里进行下类设定

<uses-permission android:name="android.permission.VIBRATE"/>

3. Notification的触发和取消

NotificationManager.notify()和NotificationManager.cancel()。

每一个Notification都有一个唯一的id,这个id是开发者来指定的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值