首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在自定义的Android通知中添加一个Chronometer并更改文本颜色

在自定义的Android通知中添加一个Chronometer并更改文本颜色
EN

Stack Overflow用户
提问于 2018-03-23 23:44:36
回答 1查看 595关注 0票数 1

我创建了一个带有自定义通知的前台服务,并希望添加一个TickListener并更改时钟的文本颜色。通知按照XML文件中的定义显示,以后无法更改。如果我更改文本颜色或添加侦听器,则不会发生任何反应。

代码语言:javascript
复制
public void onCreate() 
{
  super.onCreate();
  notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
  LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
  View layout = inflater.inflate(R.layout.service_location_gps_notification, null);
chronometer = layout.findViewById(R.id.chronometer_Service_Location_GPS_Notification);
  chronometer.setTextColor(Color.BLUE);
  chronometer.setOnChronometerTickListener(chronometerOnClickListener);
  chronometer.setText("00:00:00");
  remoteViews = new RemoteViews(getPackageName(), R.layout.service_location_gps_notification);

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
  {
    CharSequence name = getString(R.string.app_name);
    NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, NotificationManager.IMPORTANCE_DEFAULT);
    notificationChannel.setLockscreenVisibility(VISIBILITY_PUBLIC);
    notificationManager.createNotificationChannel(notificationChannel);
  }
  startForeground(NOTIFICATION_ID, getNotification());
}

private Notification getNotification() 
{
  Intent intent = new Intent(this, ServiceLocationGPS.class);
  PendingIntent servicePendingIntent = PendingIntent.getService(this, 0, intent,
        PendingIntent.FLAG_UPDATE_CURRENT);

  PendingIntent activityPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

  int drawable;
  String modus;

  if(FragmentDistanceLocation.DISTANCE_MODE) 
  {
    remoteViews.setTextViewText(R.id.textView_Service_Location_GPS_Notification_Titel, String.valueOf(this.meter) + " Meter");
    modus = "Strecken Modus";
    drawable = R.drawable.ic_directions_run_white_24dp;
    chronometer.setTextColor(getResources().getColor(R.color.ColorGreen));
  } else {
    remoteViews.setTextViewText(R.id.textView_Service_Location_GPS_Notification_Titel, this.locationname);
    modus = "HealthPoint Modus";
    drawable = R.drawable.ic_fitness_center_white_24dp;
    chronometer.setTextColor(getResources().getColor(R.color.ColorBlue));
  }
  remoteViews.setChronometer(chronometer.getId(), SystemClock.elapsedRealtime() - timerSeconds, null, true);
  NotificationCompat.Builder notificationCompatBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentIntent(activityPendingIntent)
            .addAction(R.drawable.logo_group_background, "Workout beenden",
                    servicePendingIntent)
            .setSmallIcon(drawable)
            .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
            .setCustomContentView(remoteViews)
            .setPriority(Notification.PRIORITY_HIGH)
            .setOngoing(true)
            .setVisibility(VISIBILITY_PUBLIC)
            .setWhen(System.currentTimeMillis())
            .setSubText(modus)
            .setAutoCancel(true);
  return notificationCompatBuilder.build();
}

Chronometer.OnChronometerTickListener chronometerOnClickListener = new 
Chronometer.OnChronometerTickListener()
{
  @Override
  public void onChronometerTick(Chronometer chronometer) {
    long time = SystemClock.elapsedRealtime() - chronometer.getBase();
    int h = (int) (time / 3600000);
    int m = (int) (time - h * 3600000) / 60000;
    int s = (int) (time - h * 3600000 - m * 60000) / 1000;
    String hh = h < 10 ? "0" + h : h + "";
    String mm = m < 10 ? "0" + m : m + "";
    String ss = s < 10 ? "0" + s : s + "";
    chronometer.setText(hh + ":" + mm + ":" + ss);
  }
};

这是layout_Service_Location_GPS_Notification.xml

代码语言:javascript
复制
<TextView
        android:id="@+id/textView_Service_Location_GPS_Notification_Titel"
        style="@style/TextAppearance.Compat.Notification.Title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@color/ColorPrimary"
        android:gravity="center_horizontal|center"
        android:text="Strecke"
        android:textColor="@color/ActionbarColor" />

    <Chronometer
        android:id="@+id/chronometer_Service_Location_GPS_Notification"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@color/ColorPrimary"
        android:clickable="false"
        android:fontFamily="sans-serif-thin"
        android:gravity="center_vertical|center_horizontal"
        android:longClickable="false"
        android:textColor="@color/ColorGreen"
        android:textSize="@dimen/_22sdp"
        android:textStyle="bold" />

有谁有什么想法吗?

EN

回答 1

Stack Overflow用户

发布于 2021-04-15 21:34:32

我知道这是一个非常古老的帖子,但我也在寻找答案,并找到了一个。不确定这是否会对您的特定问题有所帮助,但调用chronometer.setbase(SystemClock.elapsedRealTime())会更新任何格式更改。在您的情况下,看起来您需要设置计时器的内部时间。我不会花时间去帮助那个特定的问题,因为这是一个古老的帖子

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49453533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档