获取当前时间的android

时间:2014-06-21 20:03:11

标签: android multithreading alarm

我正在尝试为Android制作一个警报应用程序,我应该做的第一件事是不断获取系统的当前时间,所以我这样做但它只获得当前的第二个,没有更多,任何帮助? 这是代码:

public class MainActivity extends Activity {
    public Button time;
    public TextView secondview;
    public static int hours, mins, secs;

    Handler main;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        time = (Button) findViewById(R.id.button1);
        secondview = (TextView) findViewById(R.id.textView2);

        new Thread(new Runnable() {

            @Override
            public void run() {
                secondview.setText(String.valueOf(secs));
                try {
                    Thread.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        main = new Handler() {

            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                Calendar mycal = Calendar.getInstance();
                hours = mycal.get(Calendar.HOUR);
                mins = mycal.get(Calendar.MINUTE);
                secs = mycal.get(Calendar.SECOND);

                secondview.setText(String.valueOf(secs));
            }
        };
    }
}

3 个答案:

答案 0 :(得分:2)

HandlerThread是此类问题的不良选择。

要制作闹钟应用,请使用AlarmManager

您应该仔细选择要使用的计时器类型。 (ELAPSED_REALTIMERTC,....)
对于闹钟应用,RTC_WAKEUP是不错的选择。

Android developer site has training如何使用AlarmManager

报价:

// Set the alarm to start at approximately 2:00 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);

// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, alarmIntent);

还有一些用例的例子。

答案 1 :(得分:0)

Android提供两种服务方式,一种是绑定活动,一种是活动,服务也是,另一种可以实现相同的功能,但它与活动没有关系。在这里我们需要使用第二个!

public class MainActivity extends Activity {

private Button startService;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startService = (Button) findViewById(R.id.startService);
    startService.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, CountService.class);
            startService(intent);
        }
    });
}

@Override
protected void onDestroy() {
    super.onDestroy();
    Intent intent = new Intent(MainActivity.this,CountService.class);
    stopService(intent);
}}

public class CountService extends Service{

private int seconds=0;
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    new Thread(new Runnable() {
        @Override
        public void run() {
            while(seconds!=-1){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Log.i("TIME","Time  "+seconds);
                //handle seconds use Calendar and AlarmManager


                seconds++;
            }

        }
    }).start();

}

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

答案 2 :(得分:0)

我是从某个地方找到的。它使用timertask。

 public void updateTimeOnEachSecond() {
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            c = Calendar.getInstance();
            Log.d("myapp", "time changed");
            hrs = c.get(Calendar.HOUR_OF_DAY);
            min = c.get(Calendar.MINUTE);
            sec = c.get(Calendar.SECOND);

           runOnUiThread(new Runnable() {
            @Override
            public void run() {
            txt_hrs.setText(String.valueOf(hrs));
            txt_mins.setText(String.valueOf(min));
            txt_sec.setText(String.valueOf(sec));
           }
        });

        }
    }, 0, 1000);

}