【问题标题】:Android Wear custom NotificationAndroid Wear 自定义通知
【发布时间】:2014-07-30 17:40:57
【问题描述】:

当自定义通知在主屏幕上查看时,系统会使用从通知的语义数据生成的标准模板来显示它。我必须向上滑动通知,才能看到通知的自定义活动。我将“向上滑动以查看”文本显示为标准模板的标题。我的问题是,我可以用计时器代替“向上滑动查看”,计时器会随着计时器的增加而增加吗?

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());          

        }

    }

};

【问题讨论】:

  • 以上代码代表我更新通知的活动。
  • 您已经发布了部分代码(顺便说一下,它有语法错误),但问题是一样的(并且它已经在我的回答中得到了回答 - 我也测试了我的解决方案,它正常工作)。如果您有任何不想要的结果的问题,请在某处描述。也没有地方可以切换isCustomCardAcrivityvisible 标志。另一件事:你为什么要使用notifyingThreadThread,如果你在那之后用timerHandlerCustomHandler替换它......

标签: android wear-os


【解决方案1】:

您可以将“向上滑动查看”替换为您想要的任何文字 - 例如“53 秒”。要更新此值,您只需更新您的通知。

有关更新通知的更多信息:http://developer.android.com/training/notify-user/managing.html

顺便说一句。如果你想优化你的代码,上面的注释可能对你很重要:

当您需要针对同一类型多次发出通知时 事件,你应该避免发出一个全新的通知。 相反,您应该考虑更新以前的通知,或者 通过更改它的一些值或添加它,或两者兼而有之。

编辑:此外,如果您希望将此解决方案与自定义布局 Activity 一起使用,您需要防止在此 Activity 可见时刷新通知。否则,您最终会一遍又一遍地创建Activity(闪烁布局)。卡片布局中的自定义ActivityonResumeonPause 事件之间可见,因此只有当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 字段显示为秒表。而不是呈现 when 作为 时间戳,通知将显示自动更新 显示从什么时候开始的分钟和秒。显示时很有用 经过的时间(如正在进行的电话)。

【讨论】:

  • 这是因为您的自定义布局中的Activity 正在一次又一次地创建。我认为没有办法阻止它,但你可以自己处理它。如果您的Activity 可见(在onResumeonPause 之间),您应该只更新活动内容而不是更新整个通知,因为无论如何标准通知模板都不可见。如果活动暂停,您需要更新通知本身。
  • 但是 onCreate、onPause 和 onResume 每次都会被调用 :-(
  • @user1672337 只有在您更新通知时,它们才会“每次都被调用”,对吗?我的 cmets 是关于:如果您的活动可见 - 无需更新整个通知。这解决了“闪烁”活动的问题。仅当自定义活动不可见时更新您的通知。
  • 但是如何检查activity是否可见??
  • 在这种情况下 - 如果它在 onResumeonPause 之间。你需要自己弄清楚——我只能给你一个想法。最简单的方法是拥有一些像isMyCustomActivityVisible 这样的静态变量,并在onResumeonPause 中将其触发为真/假。玩一下,看看你能用它做什么:) 如果该标志设置为 true - 不要更新你的通知。很简单:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多