Check Smartphone Current Time

Asked

Viewed 145 times

0

I am always wanting to check the current time of my device and with this display a notification. But using this code, I can only catch the time I open the app

How can I do this background check, and when I arrive at the given time the app send me the notification.

Thank you in advance

 SimpleDateFormat dateFormat_hora = new SimpleDateFormat("HH:mm:ss");

    Date data = new Date();

    Calendar  cal = Calendar.getInstance();
    cal.setTime(data);
    Date data_atual = cal.getTime();

    String hora_atual = dateFormat_hora.format(data_atual);

    TextView horaatual = (TextView) findViewById(R.id.horaatual);
    horaatual.setText(hora_atual);

2 answers

2

Friend, you can solve your problem using the class Alarmmanager (https://developer.android.com/reference/android/app/AlarmManager.html).

With it it is possible to schedule the execution of a code, including with repetition within a certain period of time. In your case could be the display of the desired notification.

Here you can find an example of the use of the class:

private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

// configura o alarme para disparar as 8:30
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);

// setRepeating() para especificar um intervalo de repetição -- 20 minutos, por exemplo

alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
        1000 * 60 * 20, alarmIntent);

Source: https://developer.android.com/training/scheduling/alarms.html - with sample source code for download.

  • Response was appropriate as suggested.

0

I don’t know if I understood what you want, but if you want to have the current time in a textview you can do the following:

package mz.co.oncreate.timetest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Thread myThread = null;
    Runnable runnable = new CountDownRunner();
    myThread= new Thread(runnable);
    myThread.start();
}

public void doWork() {
    runOnUiThread(new Runnable() {
        public void run() {
            try{
                TextView horaCorrente= (TextView)findViewById(R.id.horaLbl);
                Date dt = new Date();
                int hora = dt.getHours();
                int minutos = dt.getMinutes();
                int segundos = dt.getSeconds();
                String curTime = hora + ":" + minutos + ":" + segundos;
                horaCorrente.setText(curTime);
            }catch (Exception e) {}
        }
    });
}

class CountDownRunner implements Runnable{
    public void run() {
        while(!Thread.currentThread().isInterrupted()){
            try {
                doWork();
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }catch(Exception e){
            }
        }
    }
}
}

Next you only do your implementation for the notification " before: timeCurrent.setText(curTime); "

Browser other questions tagged

You are not signed in. Login or sign up in order to post.