Android Wear自定义通知 [英] Android Wear custom Notification
问题描述
在自定义通知主屏幕上偷看,系统将显示它与它从通知的语义数据生成一个标准模板。我要刷卡的通知后,可查看该通知自定义活动。我展示'向上滑动即可查看文本作为标题标准模板。我的问题是,我可以代替向上滑动即可查看与时间计数器,计时器会增加吗?
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
字符串NS = getApplicationContext()NOTIFICATION_SERVICE。
mNotificationManager =(NotificationManager)getSystemService(NS);
callNotification(Html.fromHtml(getFormattedTime(CurrentPausedTime)),假的,假的);
螺纹notifyingThread =新主题(NULL,updateTimerTaskCustom,NotifyingServiceNew);
notifyingThread.start();}
私人无效callNotification(最终跨区跨区卷,布尔isPause,布尔pauseAction){
// TODO自动生成方法存根 displayIntent =新意图(getApplicationContext(),CustomNotification.class);
displayIntent.putExtra(exerciseTitle,暗战);
displayIntent.putExtra(持续时间,CurrentPausedTime);
displayIntent.putExtra(elepsedTime,0);
displayIntent.putExtra(isPause,isPause); displayPendingIntent = PendingIntent.getActivity(getApplicationContext(),
0,displayIntent,PendingIntent.FLAG_UPDATE_CURRENT);
意图deleteIntent =新意图(getApplicationContext(),ClearNotification.class);
deletePendingIntent = PendingIntent.getActivity(getApplicationContext(),
0,deleteIntent,PendingIntent.FLAG_UPDATE_CURRENT);
意图pauseIntent =新意图(ExerciseActionReciever.ACTION_PAUSE,
空,getApplicationContext(),ExerciseActionReciever.class);
pausePendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
0,pauseIntent,PendingIntent.FLAG_UPDATE_CURRENT); 意图stopIntent =新意图(ExerciseActionReciever.ACTION_STOP,
空,getApplicationContext(),ExerciseActionReciever.class);
stopPendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
0,stopIntent,PendingIntent.FLAG_UPDATE_CURRENT);
意图resumeIntent =新意图(ExerciseActionReciever.ACTION_RESUME,
空,getApplicationContext(),ExerciseActionReciever.class);
resumePendingIntent = PendingIntent.getBroadcast(getApplicationContext(),0,resumeIntent,PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.WearableExtender wearableExtender =
新NotificationCompat.WearableExtender()
.setHintHideIcon(真)
.setContentIcon(R.drawable.icon)
.setDisplayIntent(displayPendingIntent);
mNotifyBuilder =
新NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.icon)
.setContentTitle(+跨区)
.setDeleteIntent(deletePendingIntent)
.extend(wearableExtender)
.addAction(R.drawable.icon,恢复,resumePendingIntent)
.addAction(R.drawable.icon,停止,stopPendingIntent);}私人Runnable接口updateTimerTaskCustom =新的Runnable(){ 公共无效的run(){
timerHandlerCustom.removeCallbacks(updateTimerTaskCustom);
timerHandlerCustom.postDelayed(updateTimerTaskCustom,1000);
如果(!CustomNotification.isCustomCardAcrivityvisible)
{
CurrentPausedTime = CurrentPausedTime + 1000;
mNotifyBuilder.setContentTitle(+ Html.fromHtml(getFormattedTime(CurrentPausedTime)));
mNotificationManager.notify(NOTIFICTIONTION_ID,mNotifyBuilder.build()); } }};
您可以替换向上滑动即可查看你想要的任何文字 - 如53秒。要更新此值,则需要简单地更新您的通知。
有关更新通知的详细信息: http://developer.android.com/培训/通知用户/ managing.html
BTW。如果你想优化你的code在上面的说明可能是对你很重要:
当你需要多次发出通知的相同类型
事件中,你应该避免一个完全新的通知。
相反,你应该考虑更新previous通知,无论是
通过改变它的一些值或通过添加到它,或两者兼而有之。
块引用>修改:如果你想,此外,使用自定义布局该解决方案
活动
你需要prevent通知,刷新当此活动
可见。否则,你将最终活动
创建一遍又一遍(闪烁布局)。自活动
卡布局是onResume
和的onPause
事件,所以你需要检测并更新整个通知,只有当活动
是对用户不可见的。该simpliest的方法是使用一个静态的标志,但你也可以用其他更先进的解决方案发挥(如LocalBroadcastManager
等)来实现这一目标。公共类CustomLayoutActivity延伸活动{ 公共静态布尔isCustomCardAcrivityvisible; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.custom_layout_activity);
} @覆盖
保护无效onResume(){
super.onResume();
isCustomCardAcrivityvisible = TRUE;
} @覆盖
保护无效的onPause(){
super.onPause();
isCustomCardAcrivityvisible = FALSE;
}
}如果你是即将刷新您的通知只是检查以下内容:
如果(!CustomLayoutActivity.isCustomCardAcrivityvisible){
updateNotification();
}或者您可以使用
setUsesChronometer(布尔B)
的方法,只是显示一个计时器(而不是contextText
),将刷新你,但请注意,定时器将只显示(在Android Wear),如果你不设置自定义布局,您的卡。因此,尽管这是不是你想要什么,你可以改为考虑一下。
显示领域时,作为一个秒表。相反,当为的presenting
时间戳,该通知将显示一个自动更新
因为当分秒的显示。有用示出当
经过时间(如正在进行的电话)。
块引用>When the custom notification is peeking on the homescreen, the system displays it with a standard template that it generates from the notification's semantic data. I have to swipe the notification up, to see the custom activity for the notification. I'm showing 'Swipe up to view' text as title for standard template. My question is, Can i replace "Swipe up to view" with a time counter, which increase with timer?
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()); } } };
解决方案You can replace "Swipe up to view" with any text you want - like "53 sec". To update this value you will need to simply update your notification.
More information about updating notifications: http://developer.android.com/training/notify-user/managing.html
BTW. If you want to optimise your code the note on top might be important to you:
When you need to issue a notification multiple times for the same type of event, you should avoid making a completely new notification. Instead, you should consider updating a previous notification, either by changing some of its values or by adding to it, or both.
EDIT: If you want, in addition, to use this solution with custom layout
Activity
you need to prevent notification from refreshing when thisActivity
is visible. Otherwise you will end up withActivity
being created over and over again (blinking layout). CustomActivity
in card layout is visible between theonResume
andonPause
events, so you need to detect that and update the whole notification ONLY whenActivity
is NOT visible to the user. The simpliest way is to use a static flag, but you can also play with other more advanced solutions (likeLocalBroadcastManager
etc.) to achieve this goal.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; } }
and if you're about to refresh your notification just do following check:
if(!CustomLayoutActivity.isCustomCardAcrivityvisible) { updateNotification(); }
Alternatively you can use
setUsesChronometer(boolean b)
method, to just display a timer (instead ofcontextText
) that will be refreshed for you, but please notice that the timer will only be displayed (on Android Wear) if you will NOT set a custom layout to your card. So while this is not exactly what you want, you may consider this instead.Show the when field as a stopwatch. Instead of presenting when as a timestamp, the notification will show an automatically updating display of the minutes and seconds since when. Useful when showing an elapsed time (like an ongoing phone call).
这篇关于Android Wear自定义通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!