Ahmad Sameh
Ahmad Sameh

Reputation: 59

Getting current time continuously android

I'm trying to make an alarm app for android the first thing that i should do is to continuously get the current time of system so i did this but it only gets the current second and no more ,, any help ? here is the code :

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));
            }
        };
    }
}

Upvotes: 2

Views: 2042

Answers (3)

xyz
xyz

Reputation: 1363

I found this from somewhere. It uses 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);

}

Upvotes: 0

sundroid
sundroid

Reputation: 24

Android provide two ways of service,one is bind to activity,when activity destoyed,the service also,another can achieve the same function,but it have no connoction with activity. and here we need use the second one!

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();
}}

Upvotes: 0

ytRino
ytRino

Reputation: 1459

Handler and Thread are bad choice for this type of problem.

To make an alarm app, use AlarmManager.

You should carefully choose which type of timer to use. (ELAPSED_REALTIME, RTC, ....)
For alarm app, RTC_WAKEUP is good choice.

And Android developer site has training how to use AlarmManager.

quote:

// 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);

There's another example for some use case.

Upvotes: 2

Related Questions