7

I am pondering over how to implement a listener as to detect whenever the minute passes on my phone.

1) Handler 2) AlarmManager 3) Own thread thing

I wish for my app to run specific code every minute the clock changes, it's important to fire the same time the minute changes on my phone, otherwise I would of just used a thread with wait 60000.

basickarl
  • 37,187
  • 64
  • 214
  • 335

4 Answers4

11

Thanks to the hint of Manpreet Singh I was able to come up with the following:

BroadcastReceiver tickReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().compareTo(Intent.ACTION_TIME_TICK) == 0) {
            Log.v("Karl", "tick tock tick tock...");
        }
    }
};
registerReceiver(tickReceiver, new IntentFilter(Intent.ACTION_TIME_TICK)); // register the broadcast receiver to receive TIME_TICK

Then call the following onStop():

// unregister broadcast receiver, will get an error otherwise
if(tickReceiver!=null) 
   unregisterReceiver(tickReceiver);
Ololoking
  • 1,577
  • 4
  • 22
  • 37
basickarl
  • 37,187
  • 64
  • 214
  • 335
  • 3
    typo: BoradcastReceiver should be BroadcastReceiver Also what is the point of the if statement? This broadcast is only received when the minute changes.... – k2col May 26 '17 at 01:37
4

Assuming that the method initClock() updates the UI, then I'd suggest the following:

private BroadcastReceiver mTimeTickReceiver;

@Override
protected void onResume() {
    super.onResume();
    initClock();

    mTimeTickReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            initClock();
        }
    };
    registerReceiver(mTimeTickReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
}

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(mTimeTickReceiver);
}
k2col
  • 2,167
  • 18
  • 18
-1

create one receiver like following

public class AlarmReceiver extends BroadcastReceiver  
{
    @Override
    public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Log.e("alarmreceiver","called");

   }
}

register it in your manifest

    <receiver android:name="AlarmReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

and finally in activity write below code

        Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
        intent.setAction("packagename.ACTION");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
                    0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
                AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarm.cancel(pendingIntent);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60, pendingIntent);

thats it, run your application and log will be printed in logcat each minute

Ravi
  • 34,851
  • 21
  • 122
  • 183
  • This works just like setting Thread.sleep(60*1000), I need something that detects when the smartphones clock goes from 13:15 to 13:16 for example! – basickarl Mar 09 '15 at 14:08
  • This doesn't answer the question, he/she wants to know when the system clock changes from say 10:59:59 to 11:00:00 – k2col May 26 '17 at 01:35
-2

You can do it in own thread. It works fine. I had exactly the same requirement like you and i did this

Thread t = new Thread() {

  @Override
  public void run() {
    try {
      while (!isInterrupted()) {
        Thread.sleep(60000);
        runOnUiThread(new Runnable() {
          @Override
          public void run() {
            // updated my UI here 
          }
        });
      }
    } catch (InterruptedException e) {
    }
  }
};

t.start();

If not this you can use Timertask object as well. Both work fine

For timertask I guess this link will help

Android - Want app to perform tasks every second

Community
  • 1
  • 1
Hulk
  • 237
  • 1
  • 13
  • sorry, What is "!isInterrupted()"! – kemdo Mar 09 '15 at 04:23
  • 1
    isInterrupted() will return false every time since we are not interrupting this thread and since it returns false each time , the loop will run every minute. This method actually checks wether our thread is interrupted by any other thread or not. – Hulk Mar 09 '15 at 04:58
  • I need it as stated to fire whenever the android smartphone clock changes from for example 12:59 to 13:00. – basickarl Mar 09 '15 at 13:00
  • 1
    this solution is the worst i've ever seen. what would happen if you start the thread in the middle of a minute? you'll have a delay of 30 seconds on receiving the change – Ofek Regev Jan 21 '19 at 14:04
  • This is horrible, you're basically creating a new thread and then pushing it to the main thread – Chisko Dec 06 '19 at 00:05