Android - 希望应用程序每秒执行任务

发布于 2025-01-08 10:19:01 字数 11843 浏览 0 评论 0原文

我试图让我的倒计时应用程序执行其更新当前时间和日期 TextView 的功能,并每秒触发其 MyOnItemSelectedListener ,以便应用程序动态倒计时,而不是仅在启动 onCreate 时进行倒计时。如果有一种更有效的方法,然后快速触发 MyOnItemSelectedListener 我将不胜感激。

public class TheCount extends Activity {
TextView description=null;
TextView dates=null;
TextView times=null;
TextView output=null;
TextView cDateDisplay=null;
TextView cTimeDisplay=null;
private int mYear;
private int mMonth;
private int mDay;
private int mHour;
private int mMinute;
private int mSecond;
CountdownHelper helper=null;
String countdownId=null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.thecount);
    helper=new CountdownHelper(this);
    cTimeDisplay = (TextView)findViewById(R.id.lblCurTime);
    cDateDisplay = (TextView)findViewById(R.id.lblCurDate);

    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);
    mHour = c.get(Calendar.HOUR_OF_DAY);
    mMinute = c.get(Calendar.MINUTE);
    mSecond = c.get(Calendar.SECOND);

    Spinner spinner = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.countoptions, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    description =(TextView)findViewById(R.id.lblDescriptFed);
    dates =(TextView)findViewById(R.id.lblDateFed);
    times =(TextView)findViewById(R.id.lblTimeFed);
    output =(TextView)findViewById(R.id.lblOutput);

    countdownId=getIntent().getStringExtra(MainActivity.ID_EXTRA);
    if (countdownId!=null) {
        load();
    }

    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

    updateDisplay();

}

private void updateDisplay() {
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
//@Override
public void run() {
    updatedDisplay();
    }
},0,1000);//Update text every second

}

public class MyOnItemSelectedListener implements OnItemSelectedListener {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

        if (parent.getItemAtPosition(pos).toString().equals("Normal")){
            Toast.makeText(parent.getContext(), "Countdown format is " +
                  parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
            convertTime();
        } else if (parent.getItemAtPosition(pos).toString().equals("by Days")){
            Toast.makeText(parent.getContext(), "Countdown format " +
                      parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
                convertDays();
        } else if (parent.getItemAtPosition(pos).toString().equals("by Hours")){
            Toast.makeText(parent.getContext(), "Countdown format " +
                      parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
                convertHours();
        } else if (parent.getItemAtPosition(pos).toString().equals("by Minutes")){
            Toast.makeText(parent.getContext(), "Countdown format " +
                      parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
                convertMinutes();
        } else if (parent.getItemAtPosition(pos).toString().equals("by Seconds")){
            Toast.makeText(parent.getContext(), "Countdown format " +
                      parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
                convertSeconds();
        }

    }
    public void onNothingSelected(AdapterView parent) {
      // Do nothing.
    }
}

private void convertTime() {
    String dtCountdown = dates.getText().toString() + " " + times.getText().toString();
    String dtCurrent = cDateDisplay.getText().toString() + " " + cTimeDisplay.getText().toString();
    SimpleDateFormat  format = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");  
    try {  
        Date date = format.parse(dtCountdown);
        Date dateCur = format.parse(dtCurrent);
        long diff = dateCur.getTime() - date.getTime();

        int timeInSeconds = (int) (diff / 1000);
        int days, hours, minutes, seconds;
        days = timeInSeconds / 86400;
        timeInSeconds = timeInSeconds - (days * 86400);
        hours = timeInSeconds / 3600;
        timeInSeconds = timeInSeconds - (hours * 3600);
        minutes = timeInSeconds / 60;
        timeInSeconds = timeInSeconds - (minutes * 60);
        seconds = timeInSeconds;


        System.out.println(date); 
        if(dateCur.compareTo(date)>0){
            output.setText(String.valueOf(days) + " days " + String.valueOf(hours) +
                    " hours \n" + String.valueOf(minutes) + " minutes " + String.valueOf(seconds) + " seconds ago");
        } else {
            output.setText(String.valueOf(Math.abs(days)) + " days " + String.valueOf(Math.abs(hours)) +
                    " hours \n" + String.valueOf(Math.abs(minutes)) + " minutes " + String.valueOf(Math.abs(seconds)) + " seconds till");
        }

    } catch (ParseException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    }
}

private void convertDays() {
    String dtCountdown = dates.getText().toString() + " " + times.getText().toString();
    String dtCurrent = cDateDisplay.getText().toString() + " " + cTimeDisplay.getText().toString();
    SimpleDateFormat  format = new SimpleDateFormat("dd/MM/yyyy hh:mm");  
    try {  
        Date date = format.parse(dtCountdown);
        Date dateCur = format.parse(dtCurrent);
        long diff = dateCur.getTime() - date.getTime();

        int timeInSeconds = (int) (diff / 1000);
        int days;
        days = timeInSeconds / 86400;

        System.out.println(date); 
        if(dateCur.compareTo(date)>0){
            output.setText(String.valueOf(days) + " days ago");
        } else {
            output.setText(String.valueOf(Math.abs(days)) + " days till");
        }

    } catch (ParseException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    }
}

private void convertHours() {
    String dtCountdown = dates.getText().toString() + " " + times.getText().toString();
    String dtCurrent = cDateDisplay.getText().toString() + " " + cTimeDisplay.getText().toString();
    SimpleDateFormat  format = new SimpleDateFormat("dd/MM/yyyy hh:mm");  
    try {  
        Date date = format.parse(dtCountdown);
        Date dateCur = format.parse(dtCurrent);
        long diff = dateCur.getTime() - date.getTime();

        int timeInSeconds = (int) (diff / 1000);
        int hours;

        hours = timeInSeconds / 3600;

        System.out.println(date); 
        if(dateCur.compareTo(date)>0){
            output.setText(String.valueOf(hours) + " hours ago");
        } else {
            output.setText(String.valueOf(Math.abs(hours)) + " hours till");
        }

    } catch (ParseException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    }
}

private void convertMinutes() {
    String dtCountdown = dates.getText().toString() + " " + times.getText().toString();
    String dtCurrent = cDateDisplay.getText().toString() + " " + cTimeDisplay.getText().toString();
    SimpleDateFormat  format = new SimpleDateFormat("dd/MM/yyyy hh:mm");  
    try {  
        Date date = format.parse(dtCountdown);
        Date dateCur = format.parse(dtCurrent);
        long diff = dateCur.getTime() - date.getTime();

        int timeInSeconds = (int) (diff / 1000);
        int minutes;

        minutes = timeInSeconds / 60;

        System.out.println(date); 
        if(dateCur.compareTo(date)>0){
            output.setText(String.valueOf(minutes) + " minutes ago");
        } else {
            output.setText(String.valueOf(Math.abs(minutes)) + " minutes till");
        }

    } catch (ParseException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    }
}

private void convertSeconds() {
    String dtCountdown = dates.getText().toString() + " " + times.getText().toString();
    String dtCurrent = cDateDisplay.getText().toString() + " " + cTimeDisplay.getText().toString();
    SimpleDateFormat  format = new SimpleDateFormat("dd/MM/yyyy hh:mm");  
    try {  
        Date date = format.parse(dtCountdown);
        Date dateCur = format.parse(dtCurrent);
        long diff = dateCur.getTime() - date.getTime();

        int timeInSeconds = (int) (diff / 1000);
        int seconds;
        seconds = timeInSeconds;

        System.out.println(date); 
        if(dateCur.compareTo(date)>0){
            output.setText(String.valueOf(seconds) + " seconds ago");
        } else {
            output.setText(String.valueOf(Math.abs(seconds)) + " seconds till");
        }

    } catch (ParseException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    }
}

private void updatedDisplay() {
    TextView cDateDisplay=null;
    TextView cTimeDisplay=null;
    cTimeDisplay = (TextView)findViewById(R.id.lblCurTime);
    cDateDisplay = (TextView)findViewById(R.id.lblCurDate);
    cDateDisplay.setText(
        new StringBuilder()
                // Month is 0 based so add 1
                .append(mDay).append("/")
                .append(mMonth + 1).append("/")
                .append(mYear).append(" "));
    cTimeDisplay.setText(
            new StringBuilder()
                    .append(pad(mHour)).append(":")
                    .append(pad(mMinute)).append(":").append(pad(mSecond)));
}

private static String pad(int c) {
    if (c >= 10)
        return String.valueOf(c);
    else
        return "0" + String.valueOf(c);
}  

private void load() {
    Cursor c=helper.getById(countdownId);

    c.moveToFirst();    
    description.setText(helper.getDescription(c));
    dates.setText(helper.getDate(c));
    times.setText(helper.getTime(c) + ":00");

    c.close();
}

@Override
public void onDestroy() {
    super.onDestroy();

    helper.close();
}

@NamLe

:这会使应用程序崩溃,并在调用 onCreate() 时将其显示在 LogCat 中。

02-23 13:19:43.540: I/System.out(11667): 2012 年 6 月 24 日星期日 00:00:00 MDT 02-23 13:19:44.500:W/dalvikvm(11667):threadid = 11:线程因未捕获的异常而退出(组= 0x40a351f8) 02-23 13:19:44.510: E/AndroidRuntime(11667): 致命异常: Timer-0 02-23 13:19:44.510:E / AndroidRuntime(11667):android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触摸其视图。 02-23 13:19:44.510:E / AndroidRuntime(11667):在android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4039) 02-23 13:19:44.510:E / AndroidRuntime(11667):在android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:709) 02-23 13:19:44.510:E / AndroidRuntime(11667):在android.view.View.requestLayout(View.java:12675) 02-23 13:19:44.510:E / AndroidRuntime(11667):在android.view.View.requestLayout(View.java:12675) 02-23 13:19:44.510:E / AndroidRuntime(11667):在android.view.View.requestLayout(View.java:12675) 02-23 13:19:44.510:E / AndroidRuntime(11667):在android.view.View.requestLayout(View.java:12675) 02-23 13:19:44.510:E / AndroidRuntime(11667):在android.view.View.requestLayout(View.java:12675) 02-23 13:19:44.510:E / AndroidRuntime(11667):在android.widget.TextView.checkForRelayout(TextView.java:6773) 02-23 13:19:44.510:E / AndroidRuntime(11667):在android.widget.TextView.setText(TextView.java:3306) 02-23 13:19:44.510:E / AndroidRuntime(11667):在android.widget.TextView.setText(TextView.java:3162) 02-23 13:19:44.510:E / AndroidRuntime(11667):在android.widget.TextView.setText(TextView.java:3137) 02-23 13:19:44.510: E/AndroidRuntime(11667): 在 com.pixelcrunch.crunchtime.TheCount$1.run(TheCount.java:87) 02-23 13:19:44.510: E/AndroidRuntime(11667): 在 java.util.Timer$TimerImpl.run(Timer.java:284)

I'm trying to get my countdown app to perform its function of updating my current time and date TextView and fire off its MyOnItemSelectedListener every second so that the app counts down dynamically instead of just when onCreate is initiated. If there is a more efficient method then rapidly firing off MyOnItemSelectedListener I would appreciate the criticism.

public class TheCount extends Activity {
TextView description=null;
TextView dates=null;
TextView times=null;
TextView output=null;
TextView cDateDisplay=null;
TextView cTimeDisplay=null;
private int mYear;
private int mMonth;
private int mDay;
private int mHour;
private int mMinute;
private int mSecond;
CountdownHelper helper=null;
String countdownId=null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.thecount);
    helper=new CountdownHelper(this);
    cTimeDisplay = (TextView)findViewById(R.id.lblCurTime);
    cDateDisplay = (TextView)findViewById(R.id.lblCurDate);

    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);
    mHour = c.get(Calendar.HOUR_OF_DAY);
    mMinute = c.get(Calendar.MINUTE);
    mSecond = c.get(Calendar.SECOND);

    Spinner spinner = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.countoptions, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    description =(TextView)findViewById(R.id.lblDescriptFed);
    dates =(TextView)findViewById(R.id.lblDateFed);
    times =(TextView)findViewById(R.id.lblTimeFed);
    output =(TextView)findViewById(R.id.lblOutput);

    countdownId=getIntent().getStringExtra(MainActivity.ID_EXTRA);
    if (countdownId!=null) {
        load();
    }

    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

    updateDisplay();

}

private void updateDisplay() {
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
//@Override
public void run() {
    updatedDisplay();
    }
},0,1000);//Update text every second

}

public class MyOnItemSelectedListener implements OnItemSelectedListener {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {

        if (parent.getItemAtPosition(pos).toString().equals("Normal")){
            Toast.makeText(parent.getContext(), "Countdown format is " +
                  parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
            convertTime();
        } else if (parent.getItemAtPosition(pos).toString().equals("by Days")){
            Toast.makeText(parent.getContext(), "Countdown format " +
                      parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
                convertDays();
        } else if (parent.getItemAtPosition(pos).toString().equals("by Hours")){
            Toast.makeText(parent.getContext(), "Countdown format " +
                      parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
                convertHours();
        } else if (parent.getItemAtPosition(pos).toString().equals("by Minutes")){
            Toast.makeText(parent.getContext(), "Countdown format " +
                      parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
                convertMinutes();
        } else if (parent.getItemAtPosition(pos).toString().equals("by Seconds")){
            Toast.makeText(parent.getContext(), "Countdown format " +
                      parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
                convertSeconds();
        }

    }
    public void onNothingSelected(AdapterView parent) {
      // Do nothing.
    }
}

private void convertTime() {
    String dtCountdown = dates.getText().toString() + " " + times.getText().toString();
    String dtCurrent = cDateDisplay.getText().toString() + " " + cTimeDisplay.getText().toString();
    SimpleDateFormat  format = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");  
    try {  
        Date date = format.parse(dtCountdown);
        Date dateCur = format.parse(dtCurrent);
        long diff = dateCur.getTime() - date.getTime();

        int timeInSeconds = (int) (diff / 1000);
        int days, hours, minutes, seconds;
        days = timeInSeconds / 86400;
        timeInSeconds = timeInSeconds - (days * 86400);
        hours = timeInSeconds / 3600;
        timeInSeconds = timeInSeconds - (hours * 3600);
        minutes = timeInSeconds / 60;
        timeInSeconds = timeInSeconds - (minutes * 60);
        seconds = timeInSeconds;


        System.out.println(date); 
        if(dateCur.compareTo(date)>0){
            output.setText(String.valueOf(days) + " days " + String.valueOf(hours) +
                    " hours \n" + String.valueOf(minutes) + " minutes " + String.valueOf(seconds) + " seconds ago");
        } else {
            output.setText(String.valueOf(Math.abs(days)) + " days " + String.valueOf(Math.abs(hours)) +
                    " hours \n" + String.valueOf(Math.abs(minutes)) + " minutes " + String.valueOf(Math.abs(seconds)) + " seconds till");
        }

    } catch (ParseException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    }
}

private void convertDays() {
    String dtCountdown = dates.getText().toString() + " " + times.getText().toString();
    String dtCurrent = cDateDisplay.getText().toString() + " " + cTimeDisplay.getText().toString();
    SimpleDateFormat  format = new SimpleDateFormat("dd/MM/yyyy hh:mm");  
    try {  
        Date date = format.parse(dtCountdown);
        Date dateCur = format.parse(dtCurrent);
        long diff = dateCur.getTime() - date.getTime();

        int timeInSeconds = (int) (diff / 1000);
        int days;
        days = timeInSeconds / 86400;

        System.out.println(date); 
        if(dateCur.compareTo(date)>0){
            output.setText(String.valueOf(days) + " days ago");
        } else {
            output.setText(String.valueOf(Math.abs(days)) + " days till");
        }

    } catch (ParseException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    }
}

private void convertHours() {
    String dtCountdown = dates.getText().toString() + " " + times.getText().toString();
    String dtCurrent = cDateDisplay.getText().toString() + " " + cTimeDisplay.getText().toString();
    SimpleDateFormat  format = new SimpleDateFormat("dd/MM/yyyy hh:mm");  
    try {  
        Date date = format.parse(dtCountdown);
        Date dateCur = format.parse(dtCurrent);
        long diff = dateCur.getTime() - date.getTime();

        int timeInSeconds = (int) (diff / 1000);
        int hours;

        hours = timeInSeconds / 3600;

        System.out.println(date); 
        if(dateCur.compareTo(date)>0){
            output.setText(String.valueOf(hours) + " hours ago");
        } else {
            output.setText(String.valueOf(Math.abs(hours)) + " hours till");
        }

    } catch (ParseException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    }
}

private void convertMinutes() {
    String dtCountdown = dates.getText().toString() + " " + times.getText().toString();
    String dtCurrent = cDateDisplay.getText().toString() + " " + cTimeDisplay.getText().toString();
    SimpleDateFormat  format = new SimpleDateFormat("dd/MM/yyyy hh:mm");  
    try {  
        Date date = format.parse(dtCountdown);
        Date dateCur = format.parse(dtCurrent);
        long diff = dateCur.getTime() - date.getTime();

        int timeInSeconds = (int) (diff / 1000);
        int minutes;

        minutes = timeInSeconds / 60;

        System.out.println(date); 
        if(dateCur.compareTo(date)>0){
            output.setText(String.valueOf(minutes) + " minutes ago");
        } else {
            output.setText(String.valueOf(Math.abs(minutes)) + " minutes till");
        }

    } catch (ParseException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    }
}

private void convertSeconds() {
    String dtCountdown = dates.getText().toString() + " " + times.getText().toString();
    String dtCurrent = cDateDisplay.getText().toString() + " " + cTimeDisplay.getText().toString();
    SimpleDateFormat  format = new SimpleDateFormat("dd/MM/yyyy hh:mm");  
    try {  
        Date date = format.parse(dtCountdown);
        Date dateCur = format.parse(dtCurrent);
        long diff = dateCur.getTime() - date.getTime();

        int timeInSeconds = (int) (diff / 1000);
        int seconds;
        seconds = timeInSeconds;

        System.out.println(date); 
        if(dateCur.compareTo(date)>0){
            output.setText(String.valueOf(seconds) + " seconds ago");
        } else {
            output.setText(String.valueOf(Math.abs(seconds)) + " seconds till");
        }

    } catch (ParseException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    }
}

private void updatedDisplay() {
    TextView cDateDisplay=null;
    TextView cTimeDisplay=null;
    cTimeDisplay = (TextView)findViewById(R.id.lblCurTime);
    cDateDisplay = (TextView)findViewById(R.id.lblCurDate);
    cDateDisplay.setText(
        new StringBuilder()
                // Month is 0 based so add 1
                .append(mDay).append("/")
                .append(mMonth + 1).append("/")
                .append(mYear).append(" "));
    cTimeDisplay.setText(
            new StringBuilder()
                    .append(pad(mHour)).append(":")
                    .append(pad(mMinute)).append(":").append(pad(mSecond)));
}

private static String pad(int c) {
    if (c >= 10)
        return String.valueOf(c);
    else
        return "0" + String.valueOf(c);
}  

private void load() {
    Cursor c=helper.getById(countdownId);

    c.moveToFirst();    
    description.setText(helper.getDescription(c));
    dates.setText(helper.getDate(c));
    times.setText(helper.getTime(c) + ":00");

    c.close();
}

@Override
public void onDestroy() {
    super.onDestroy();

    helper.close();
}

}

@NamLe: This crashes the app and brings this up in the LogCat when onCreate() is called.

02-23 13:19:43.540: I/System.out(11667): Sun Jun 24 00:00:00 MDT 2012
02-23 13:19:44.500: W/dalvikvm(11667): threadid=11: thread exiting with uncaught exception (group=0x40a351f8)
02-23 13:19:44.510: E/AndroidRuntime(11667): FATAL EXCEPTION: Timer-0
02-23 13:19:44.510: E/AndroidRuntime(11667): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
02-23 13:19:44.510: E/AndroidRuntime(11667): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4039)
02-23 13:19:44.510: E/AndroidRuntime(11667): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:709)
02-23 13:19:44.510: E/AndroidRuntime(11667): at android.view.View.requestLayout(View.java:12675)
02-23 13:19:44.510: E/AndroidRuntime(11667): at android.view.View.requestLayout(View.java:12675)
02-23 13:19:44.510: E/AndroidRuntime(11667): at android.view.View.requestLayout(View.java:12675)
02-23 13:19:44.510: E/AndroidRuntime(11667): at android.view.View.requestLayout(View.java:12675)
02-23 13:19:44.510: E/AndroidRuntime(11667): at android.view.View.requestLayout(View.java:12675)
02-23 13:19:44.510: E/AndroidRuntime(11667): at android.widget.TextView.checkForRelayout(TextView.java:6773)
02-23 13:19:44.510: E/AndroidRuntime(11667): at android.widget.TextView.setText(TextView.java:3306)
02-23 13:19:44.510: E/AndroidRuntime(11667): at android.widget.TextView.setText(TextView.java:3162)
02-23 13:19:44.510: E/AndroidRuntime(11667): at android.widget.TextView.setText(TextView.java:3137)
02-23 13:19:44.510: E/AndroidRuntime(11667): at com.pixelcrunch.crunchtime.TheCount$1.run(TheCount.java:87)
02-23 13:19:44.510: E/AndroidRuntime(11667): at java.util.Timer$TimerImpl.run(Timer.java:284)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青春有你 2025-01-15 10:19:01

尝试使用如下所示的UpdateDisplay函数:

private void updateDisplay() {
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMinute = c.get(Calendar.MINUTE);
            mSecond = c.get(Calendar.SECOND);

            cDateDisplay.setText(new StringBuilder()
                // Month is 0 based so add 1
                .append(mDay).append("/")
                .append(mMonth + 1).append("/")
                .append(mYear).append(" "));

            cTimeDisplay.setText(
                new StringBuilder()
                    .append(pad(mHour)).append(":")
                    .append(pad(mMinute)).append(":").append(pad(mSecond))
            );
        }

    },0,1000);//Update text every second
}

最后,您必须在onCreate()函数中调用updateDisplay()
我希望这段代码能帮助你完成你想要的!

Try to use UpdateDisplay function like below:

private void updateDisplay() {
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);
            mHour = c.get(Calendar.HOUR_OF_DAY);
            mMinute = c.get(Calendar.MINUTE);
            mSecond = c.get(Calendar.SECOND);

            cDateDisplay.setText(new StringBuilder()
                // Month is 0 based so add 1
                .append(mDay).append("/")
                .append(mMonth + 1).append("/")
                .append(mYear).append(" "));

            cTimeDisplay.setText(
                new StringBuilder()
                    .append(pad(mHour)).append(":")
                    .append(pad(mMinute)).append(":").append(pad(mSecond))
            );
        }

    },0,1000);//Update text every second
}

Finally, you must to call updateDisplay() in onCreate() function.
I hope this code would help you finish what you want !

眼泪也成诗 2025-01-15 10:19:01

要在主线程上运行它:

        new Timer().schedule(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    yourMethodOnTheMainThread();
                }
            });
        }
    },0,10000);

To run it on the main thread:

        new Timer().schedule(new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    yourMethodOnTheMainThread();
                }
            });
        }
    },0,10000);
任性一次 2025-01-15 10:19:01

您可以使用 sendMessageAtTime()postAtTime() 稍后向自己发送一条消息,然后进行处理。

了解更多信息。

You can either use sendMessageAtTime() or postAtTime() to send yourself a message a second in the future, and handle it then.

For more information.

萤火眠眠 2025-01-15 10:19:01

也许您还可以使用 Timer/TimerTask 重复触发操作: https:// developer.android.com/reference/java/util/Timer.html

Maybe you can also use Timer/TimerTask to fire a operation repeatedly: https://developer.android.com/reference/java/util/Timer.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文