当自定义通知在主屏幕上窥视时,系统会使用从通知的语义数据生成的标准模板显示它。我必须向上滑动通知,才能看到通知的自定义活动。我正在显示“向上滑动以查看”文本作为标准模板的标题。我的问题是,我可以用时间计数器替换“向上滑动查看”,计时器会增加吗?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String ns = getApplicationContext().NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager) getSystemService(ns);
callNotification(Html.fromHtml(getFormattedTime(CurrentPausedTime)),false,false);
Thread notifyingThread = new Thread(null, updateTimerTaskCustom, "NotifyingServiceNew");
notifyingThread.start();
}
private void callNotification(final Spanned spanned,boolean isPause,boolean pauseAction) {
// TODO Auto-generated method stub
displayIntent = new Intent(getApplicationContext(), CustomNotification.class);
displayIntent.putExtra("exerciseTitle", "Running");
displayIntent.putExtra("duration", CurrentPausedTime);
displayIntent.putExtra("elepsedTime", 0);
displayIntent.putExtra("isPause", isPause);
displayPendingIntent = PendingIntent.getActivity(getApplicationContext(),
0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent deleteIntent = new Intent(getApplicationContext(), ClearNotification.class);
deletePendingIntent = PendingIntent.getActivity(getApplicationContext(),
0, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent pauseIntent = new Intent(ExerciseActionReciever.ACTION_PAUSE,
null,getApplicationContext(), ExerciseActionReciever.class);
pausePendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
0, pauseIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent stopIntent =new Intent(ExerciseActionReciever.ACTION_STOP,
null,getApplicationContext(), ExerciseActionReciever.class);
stopPendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
0, stopIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent resumeIntent = new Intent(ExerciseActionReciever.ACTION_RESUME,
null, getApplicationContext(), ExerciseActionReciever.class);
resumePendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, resumeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.WearableExtender wearableExtender =
new NotificationCompat.WearableExtender()
.setHintHideIcon(true)
.setContentIcon(R.drawable.icon)
.setDisplayIntent(displayPendingIntent);
mNotifyBuilder =
new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.icon)
.setContentTitle(""+spanned)
.setDeleteIntent(deletePendingIntent)
.extend(wearableExtender)
.addAction(R.drawable.icon, "Resume", resumePendingIntent)
.addAction(R.drawable.icon, "Stop", stopPendingIntent);
}
private Runnable updateTimerTaskCustom = new Runnable() {
public void run() {
timerHandlerCustom.removeCallbacks(updateTimerTaskCustom);
timerHandlerCustom.postDelayed(updateTimerTaskCustom, 1000);
if(!CustomNotification.isCustomCardAcrivityvisible )
{
CurrentPausedTime = CurrentPausedTime+1000;
mNotifyBuilder.setContentTitle(""+Html.fromHtml(getFormattedTime(CurrentPausedTime)));
mNotificationManager.notify(NOTIFICTIONTION_ID , mNotifyBuilder.build());
}
}
};
答案 0 :(得分:1)
您可以将“向上扫视”替换为您想要的任何文字 - 例如“53秒”。要更新此值,您只需更新通知即可。
有关更新通知的详细信息:http://developer.android.com/training/notify-user/managing.html
顺便说一句。如果您想优化代码,那么顶部的注释可能对您很重要:
当您需要为同一类型多次发出通知时 对于事件,您应该避免发出全新的通知。 相反,您应该考虑更新以前的通知 通过改变它的一些值或添加它,或两者兼而有之。
编辑:此外,如果您希望将此解决方案与自定义布局Activity
一起使用,则需要阻止在Activity
可见时刷新通知。否则,最终会一次又一次地创建Activity
(闪烁布局)。卡片布局中的自定义Activity
在onResume
和onPause
事件之间可见,因此您需要检测到该内容并仅在用户看不到Activity
时更新整个通知。最简单的方法是使用静态标志,但您也可以使用其他更高级的解决方案(如LocalBroadcastManager
等)来实现此目标。
public class CustomLayoutActivity extends Activity {
public static boolean isCustomCardAcrivityvisible;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_layout_activity);
}
@Override
protected void onResume() {
super.onResume();
isCustomCardAcrivityvisible = true;
}
@Override
protected void onPause() {
super.onPause();
isCustomCardAcrivityvisible = false;
}
}
如果您要刷新通知,请执行以下检查:
if(!CustomLayoutActivity.isCustomCardAcrivityvisible) {
updateNotification();
}
或者你可以使用setUsesChronometer(boolean b)
方法,只显示一个会为你刷新的计时器(而不是contextText
),但请注意,计时器只会如果您不为卡设置自定义布局,则显示(在Android Wear上)。所以虽然这不是你想要的,但你可以考虑这个。
将when字段显示为秒表。而不是作为一个时候呈现 时间戳,通知将显示自动更新 显示自何时起的分钟和秒数。显示时很有用 已用时间(如正在进行的通话)。