【发布时间】:2012-12-16 03:40:43
【问题描述】:
我想在自定义的时间内显示一条消息或通知,在本例中为不到 2 秒。
我尝试了两种方法,但都没有真正的帮助:
1. 通过 Toast 显示消息并通过LENGTH_SHORT 设置持续时间,这显然定义了 2 秒的硬编码持续时间。 -> 失败
2. 使用SetTicker 例程创建NotificationCompat.Builder,并在一定时间后取消通知。 -> 通知(我并不真正需要)在给定时间后消失,但不幸的是,自动收报机会停留更长的时间。 :(
private void SetNotification(CharSequence aCharSeq)
{
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),
m_RandNotificationID, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setTicker(aCharSeq)
.setContentTitle(aCharSeq).setContentText(aCharSeq).setSmallIcon(R.drawable.sound)
.setContentIntent(contentIntent);
Notification noti = builder.build();
m_NotMan.notify(m_RandNotificationID, noti);
new Timer().schedule(CancelAction, 1000L);
}
TimerTask CancelAction = new TimerTask()
{
public void run()
{
m_NotMan.cancel(m_RandNotificationID);
}
};
您的任何想法都会有所帮助。 :)
新年快乐
克里斯
【问题讨论】:
-
为什么吐司会失败?
-
您无法设置自定义持续时间。
SetDuration只允许您在 LENGTH_SHORT(2.0 秒)和 LENGTH_LONG(3.5 秒?)之间进行选择
标签: android notifications toast duration