public void onReceive(Context context, Intent intent) {

    Log.d("TimeSetReceiver", "onReceive");

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.my_widget);

    ComponentName thisWidget; // 组件名
    AppWidgetManager manager; // AppWidget管理器

    thisWidget = new ComponentName(context, CalendarWidget.class);
    manager = AppWidgetManager.getInstance(context);

    Calendar calendar = Calendar.getInstance();

    int day = calendar.get(Calendar.DAY_OF_MONTH);
    int month = calendar.get(Calendar.MONTH) + 1;
    int week = calendar.get(Calendar.DAY_OF_WEEK) - 1;
    Lunar lunar = new Lunar(calendar);

    Log.d("TimeSetReceiver", month + "," + day + lunar.toString());

    String month_week = month + "月" + "          周" + weekArray[week];
    views.setTextViewText(R.id.month_week, month_week);
    views.setTextViewText(R.id.nongli, lunar.toString());
    views.setTextViewText(R.id.day, Html.fromHtml("<B>" + String.valueOf(day) + "</B>"));

    manager.updateAppWidget(thisWidget, views);

    Log.d("TimeSetReceiver", "updateAppWidget finish");
  }
Ejemplo n.º 2
0
    /** The key method of <code>Cell</code>: how to draw date number on the cell. */
    @Override
    public void paintComponent(Graphics g) {
      super.paintComponent(g);

      if (isHeader()) return; // Do not render the header

      if (day > 0) {
        AttributedString dayAttrStr;
        Font font = MonthCalendarImpl.this.getFont();
        try {
          calendar.set(Calendar.DATE, day);

          String dayStr = Integer.toString(day);
          dayAttrStr = new AttributedString(dayStr);
          dayAttrStr.addAttribute(
              TextAttribute.FONT,
              isToday(calendar)
                  ? font.deriveFont(Font.BOLD, font.getSize() + SHRINK + SHRINK)
                  : font.deriveFont(Font.PLAIN, font.getSize() + SHRINK + 3));

          Graphics2D g2 = (Graphics2D) g;
          TextLayout textLayout =
              new TextLayout(dayAttrStr.getIterator(), g2.getFontRenderContext());
          int width = textLayout.getBounds().getBounds().width;

          Dimension size = getSize();
          int x = size.width > width ? (size.width - width) / 2 : 0;

          g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
          // Draw in the center
          g2.drawString(dayAttrStr.getIterator(), x, (int) (textLayout.getAscent()));
          int month = calendar.get(Calendar.MONTH) + 1;
          int day = calendar.get(Calendar.DAY_OF_MONTH);

          String normalDate =
              (month < 10 ? "0" + month : month) + "" + (day < 10 ? "0" + day : day);

          String lunarInfo = "";
          if (checkNormalFestival(normalDate)) {
            lunarInfo = CodeConstants.NORMAL_FESTIVAL.get(normalDate);
            setForeground(Color.blue);
          } else if (checkDefineFestival(normalDate)) {
            lunarInfo = CodeConstants.DEFINE_FESTIVAL.get(normalDate);
            setForeground(Color.green);
          } else if (checkChineseYear(month, day)) {
            lunarInfo = CodeConstants.CHINESE_FESTIVAL.get("1299");
            setForeground(Color.red);
          } else {
            // lunar date
            Lunar lunar = new Lunar(calendar);
            String lunarDate = lunar.getNumericMD();
            if (checkChineseFestival(lunarDate)) {
              lunarInfo = CodeConstants.CHINESE_FESTIVAL.get(lunarDate);
              setForeground(Color.MAGENTA);
            } else {
              if (checkUnWorkFestival(normalDate + "W") || checkUnWorkFestival(lunarDate + "Y")) {
                setForeground(Color.red);
              }
              lunarInfo = lunar.toString();
            }
          }

          AttributedString lunarStr = new AttributedString(lunarInfo);
          lunarStr.addAttribute(
              TextAttribute.FONT,
              isToday(calendar) ? font.deriveFont(Font.PLAIN, font.getSize()) : font);
          TextLayout textLayout2 =
              new TextLayout(lunarStr.getIterator(), g2.getFontRenderContext());

          // Resume date
          calendar.set(Calendar.DATE, todayInMonth);
          int width2 = textLayout2.getBounds().getBounds().width;

          int x2 = size.width > width ? (size.width - width2) / 2 : 0;

          g2.drawString(
              lunarStr.getIterator(),
              x2,
              (int)
                  (textLayout.getAscent()
                      + textLayout.getDescent()
                      + textLayout.getLeading()
                      + textLayout2.getAscent()));
        } catch (ParseException e) {
          e.printStackTrace();
        }
      }
    }