From d707d4178de45954dfd6096ac54550584e1e4ede Mon Sep 17 00:00:00 2001 From: Jacques Foucry Date: Thu, 25 Aug 2016 17:12:47 +0200 Subject: [PATCH] Mise en place du TimeManager --- app/src/main/AndroidManifest.xml | 1 + .../pilldroid/MedicamentListActivity.java | 2 + .../net/foucry/pilldroid/TimeService.java | 56 +++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 app/src/main/java/net/foucry/pilldroid/TimeService.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2580491..ebbf6f2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -43,6 +43,7 @@ + \ No newline at end of file diff --git a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java index 0ee95fc..aeb097e 100644 --- a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java @@ -71,6 +71,8 @@ public class MedicamentListActivity extends AppCompatActivity { toolbar.setTitle(getTitle()); } + startService(new Intent(this, TimeService.class)); + /*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override diff --git a/app/src/main/java/net/foucry/pilldroid/TimeService.java b/app/src/main/java/net/foucry/pilldroid/TimeService.java new file mode 100644 index 0000000..45551ee --- /dev/null +++ b/app/src/main/java/net/foucry/pilldroid/TimeService.java @@ -0,0 +1,56 @@ +package net.foucry.pilldroid; + +import android.app.Service; +import android.content.Intent; +import android.os.Handler; +import android.os.IBinder; +import android.widget.Toast; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Timer; +import java.util.TimerTask; + +/** + * Created by jacques on 22/08/16. + */ +public class TimeService extends Service { + public static final long NOTIFY_INTERVAL = 10 *1000; + + private Handler mHandler = new Handler(); + private Timer mTimer = null; + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + @Override + public void onCreate() { + if(mTimer != null) { + mTimer.cancel(); + } else { + mTimer = new Timer(); + } + + mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(),0, NOTIFY_INTERVAL); + } + + class TimeDisplayTimerTask extends TimerTask { + @Override + public void run() { + mHandler.post(new Runnable() { + + @Override + public void run() { + Toast.makeText(getApplicationContext(),getDateTime(), Toast.LENGTH_SHORT).show(); + } + }); + } + + private String getDateTime() { + SimpleDateFormat sdf = new SimpleDateFormat("[yyyy/MM/dd - HH:mm:ss]"); + return sdf.format(new Date()); + } + } +}