Android 倒计时小部件

Android 倒计时小部件,android,Android,我有一个小部件,可以倒计时到指定的日期。例如:12月10日是两天。 我想展示一下还剩多少小时甚至几分钟。怎么做?请帮帮我 代码: 你可以用很多不同的方法来做这件事。以下是1: long theTimeYourCountingTo = 134264575674567; long currentTime = System.currentTimeMillis(); long timeleft = theTimeYourCountingTo - currentTime; String format

我有一个小部件,可以倒计时到指定的日期。例如:12月10日是两天。 我想展示一下还剩多少小时甚至几分钟。怎么做?请帮帮我

代码:


你可以用很多不同的方法来做这件事。以下是1:

long theTimeYourCountingTo = 134264575674567;
long currentTime = System.currentTimeMillis();

long timeleft = theTimeYourCountingTo  - currentTime;

String formatteedTimeLeft = formatTime(timeleft);

private String formatTime(long millis) {
          String output = "";
          long seconds = millis / 1000;
          long minutes = seconds / 60;
          long hours = minutes / 60;
          long days = hours / 24;
          seconds = seconds % 60;
          minutes = minutes % 60;
          hours = hours % 24;

          String secondsD = String.valueOf(seconds);
          String minutesD = String.valueOf(minutes);
          String hoursD = String.valueOf(hours);

          if (seconds < 10)
            secondsD = "0" + seconds;
          if (minutes < 10)
            minutesD = "0" + minutes;
          if (hours < 10){
            //hoursD = "0" + hours;
          }

          if( days > 0 ){
              output = days +"d ";
          }
              output += hoursD + "h " + minutesD + "m " + secondsD + "s";

          return output;
    }
long the time your counting to=134264575674567;
长currentTime=System.currentTimeMillis();
long timeleft=您计数到当前时间的时间;
String formattedtimeleft=formatTime(timeleft);
专用字符串格式化时间(长毫秒){
字符串输出=”;
长秒=毫秒/1000;
长分钟=秒/60;
长时间=分钟/60;
长日=小时/24;
秒=秒%60;
分钟=分钟%60;
小时数=小时数%24;
String secondsD=String.valueOf(秒);
String minutesD=String.valueOf(分钟);
String hoursD=String.valueOf(小时);
如果(秒<10)
secondsD=“0”+秒;
如果(分钟<10)
分钟数d=“0”+分钟;
如果(小时<10){
//hoursD=“0”+小时;
}
如果(天数>0){
输出=天数+d;
}
输出+=hoursD+“h”+分钟d+“m”+秒sd+“s”;
返回输出;
}

如何在小部件上显示剩余时间?在我以这种方式完成之前:remoteview.setTextViewText(R.id.xmas,“+days);UpdateAppWidgetManager.updateAppWidget(此小部件,远程视图);类型MainActivity的方法formatTime(long)未定义
long theTimeYourCountingTo = 134264575674567;
long currentTime = System.currentTimeMillis();

long timeleft = theTimeYourCountingTo  - currentTime;

String formatteedTimeLeft = formatTime(timeleft);

private String formatTime(long millis) {
          String output = "";
          long seconds = millis / 1000;
          long minutes = seconds / 60;
          long hours = minutes / 60;
          long days = hours / 24;
          seconds = seconds % 60;
          minutes = minutes % 60;
          hours = hours % 24;

          String secondsD = String.valueOf(seconds);
          String minutesD = String.valueOf(minutes);
          String hoursD = String.valueOf(hours);

          if (seconds < 10)
            secondsD = "0" + seconds;
          if (minutes < 10)
            minutesD = "0" + minutes;
          if (hours < 10){
            //hoursD = "0" + hours;
          }

          if( days > 0 ){
              output = days +"d ";
          }
              output += hoursD + "h " + minutesD + "m " + secondsD + "s";

          return output;
    }