有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java添加了一个计时器监听器,并在定制的Android通知中更改文本颜色

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

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

<TextView
        安卓:id="@+id/textView_Service_Location_GPS_Notification_Titel"
        style="@style/TextAppearance.Compat.Notification.Title"
        安卓:layout_width="match_parent"
        安卓:layout_height="match_parent"
        安卓:layout_weight="1"
        安卓:background="@color/ColorPrimary"
        安卓:gravity="center_horizontal|center"
        安卓:text="Strecke"
        安卓:textColor="@color/ActionbarColor" />

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

有人有什么想法吗


共 (1) 个答案

  1. # 1 楼答案

    我知道这是一个非常古老的帖子,但我也在寻找答案,并找到了答案。不确定这是否有助于解决您的特定问题,但请致电计时器。setbase(SystemClock.elapsedRealTime())更新了所有格式更改。在你的情况下,你似乎需要设置计时器的内部时间。我不会花时间去帮助那个特定的问题,因为这是一个老帖子