From 9f7e7045e1e002bdc9e0c5e63bf3ac6cb93acd50 Mon Sep 17 00:00:00 2001 From: Jacques Foucry Date: Thu, 23 Jun 2016 17:12:25 +0200 Subject: [PATCH] Nouvelle technique pour l'alarme avec notification --- .../pilldroid/MedicamentListActivity.java | 33 +++++++++++++++---- .../pilldroid/NotificationPublisher.java | 28 ++++++++++++++++ 2 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java diff --git a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java index 166bb0f..2f481ed 100644 --- a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java @@ -1,6 +1,7 @@ package net.foucry.pilldroid; import android.app.AlarmManager; +import android.app.Notification; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; @@ -8,6 +9,7 @@ import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; +import android.os.SystemClock; import android.support.annotation.NonNull; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; @@ -78,7 +80,7 @@ public class MedicamentListActivity extends AppCompatActivity { // Register for alarm - RegisterAlarmBroadcast(); +// RegisterAlarmBroadcast(); dbHelper = new DBHelper(this); dbMedoc = new DBMedoc(this); @@ -176,13 +178,14 @@ public class MedicamentListActivity extends AppCompatActivity { public void onPause() { super.onPause(); - alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, pendingIntent); +// alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, pendingIntent); + scheduleNotification(getNotification("10 second delay"), 10000); } - protected void onDestroy() { +/* protected void onDestroy() { unregisterReceiver(mReceiver); super.onDestroy(); - } + }*/ public void scanNow(View view) { Intent intent = new Intent("com.google.zxing.client.android.SCAN"); @@ -258,8 +261,26 @@ public class MedicamentListActivity extends AppCompatActivity { recyclerView.setAdapter(mAdapter); } + private void scheduleNotification(Notification notification, int delay) { + Intent notificationIntent = new Intent(this, NotificationPublisher.class); + notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID,1); + notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification); + PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT); + + long futureInMillis = SystemClock.elapsedRealtime() + delay; + AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); + alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent); + } + + private Notification getNotification(String content) { + Notification.Builder builder = new Notification.Builder(this); + builder.setContentTitle("Scheduled Notification"); + builder.setContentText(content); + builder.setSmallIcon(R.mipmap.ic_launcher); + return builder.build(); + } // Received Alarm, display a toast - private void RegisterAlarmBroadcast() { +/* private void RegisterAlarmBroadcast() { mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { @@ -274,7 +295,7 @@ public class MedicamentListActivity extends AppCompatActivity { private void UnregisterAlarmBroadcast(){ alarmManager.cancel(pendingIntent); getBaseContext().unregisterReceiver(mReceiver); - } + }*/ /** * SimpleItemRecyclerViewAdapter */ diff --git a/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java b/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java new file mode 100644 index 0000000..a908fb0 --- /dev/null +++ b/app/src/main/java/net/foucry/pilldroid/NotificationPublisher.java @@ -0,0 +1,28 @@ +package net.foucry.pilldroid; + +import android.app.Notification; +import android.app.NotificationManager; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.os.Vibrator; + +/** + * Created by jfoucry on 6/23/16. + */ +public class NotificationPublisher extends BroadcastReceiver { + + public static String NOTIFICATION_ID = "notification-id"; + public static String NOTIFICATION = "notification"; + + public void onReceive(Context context, Intent intent) { + NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + + Notification notification = intent.getParcelableExtra(NOTIFICATION); + int id = intent.getIntExtra(NOTIFICATION_ID,0); + notificationManager.notify(id, notification); + Vibrator vibrator = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE); + vibrator.vibrate(400); + + } +}