在android中倒计时器

时间:2012-10-24 12:05:33

标签: android android-notifications

我是android的新手。我有一个可以用于考试管理的应用程序。用户可以插入每个科目的日期,应用程序将为每个科目提供时间(以小时为单位)。有倒数计时器,这将是从为每个主题分配的时间开始(例如,48小时)并且应该变为零。当它达到零时,应该播放警报,表明分配给该主题的时间已经结束。这是我的代码。

public class Alarmpage extends Activity{

    TextView hourshow,minshow,secshow;
    SQLiteDatabase database_read;
    sampleDatabase samp;
    Cursor cur;
    Handler handler;
    int initStart;
    Runnable updater;
    int  t;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alarm);
        hourshow=(TextView)findViewById(R.id.hr);
        samp=new sampleDatabase(getApplicationContext());
        database_read=samp.getReadableDatabase();
        minshow=(TextView)findViewById(R.id.min);
        secshow=(TextView)findViewById(R.id.sec);
        cur=database_read.query(sampleDatabase.TABLE_SEC, null, null, null, null, null, null);
    }

    public void onResume() {

        super.onResume();
        if(cur.moveToFirst())
            {
                 t=cur.getInt(3);
                 Log.e("time", String.valueOf(t));
            }
        handler=new Handler();
        initStart = (int) SystemClock.elapsedRealtime();
        Log.e("init", String.valueOf(initStart));
        updater = new Runnable() {

            @Override
            public void run() {

                // TODO Auto-generated method stub
                 int sec,minute,hour;
                  int diff = t*60*60;
                Log.e("time in sec", String.valueOf(diff));
                System.out.println(diff);


                /*if(diff>=1)
                {
                     hour= diff/3600;
                }
                else {hour = 00; }
                hourshow.setText(String.valueOf(hour));
                Log.e("diff after hour", String.valueOf(diff));
                Log.e("hour", String.valueOf(hour));
                minute = (diff % 3600) / 60;


                minshow.setText(":"+String.valueOf(minute));
                    sec= (diff % 60);

                secshow.setText(":"+String.valueOf(sec));*/
                if (diff >= 1) {
                    sec =  (int) (diff%60);
                } else {
                    sec=00;
                }
                secshow.setText(":"+String.valueOf(sec));
                diff = diff/60;
                if (diff >= 1) {
                    minute =  (int) (diff%60);
                } else {
                    minute=00;
                }
                minshow.setText(":"+String.valueOf(minute));
                diff = diff/60;
                if (diff >= 1) {
                    hour =  (int) (diff%24);
                } else {hour = 00; }
                hourshow.setText(String.valueOf(hour));
                t=(t*60*60)-1;
                handler.postDelayed(this, 1000);
            }
        };
        handler.post(updater);
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        handler.removeCallbacks(updater);
    }
}

但它显示了一些值。但它们不正确。请帮助我。我不明白这段代码中的错误。谢谢提前

0 个答案:

没有答案