Check current smartphone time

0

I always want to check the current time on my device and display a notification. But using this code, I can only get the time I open the app

How can I do this background check, and when it arrives at the specified time the app sends 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);
    
asked by anonymous 31.05.2016 / 01:57

2 answers

2

Friend, you can solve your problem using the AlarmManager class ( link ).

It is possible to schedule the execution of a code, even with repetition within a certain period of time. In your case it could be the display of the notification you want.

Here is an example of using 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: link - with sample source code for download.

    
31.05.2016 / 12:29
0

I do not know if I understood correctly what you want, but if it is 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 have to make your implementation for the notification "before: timeCurrent.setText (curTime);"

    
31.05.2016 / 11:07