From 4d99698930c6ed51a3d3dc3c87b3fb19f22fc278 Mon Sep 17 00:00:00 2001 From: Jacques Foucry Date: Thu, 23 Jun 2016 06:01:38 +0200 Subject: [PATCH] =?UTF-8?q?Ajout=20du=20d=C3=A9but=20des=20notifications?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pilldroid/MedicamentListActivity.java | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java index 37e3637..166bb0f 100644 --- a/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java +++ b/app/src/main/java/net/foucry/pilldroid/MedicamentListActivity.java @@ -1,8 +1,12 @@ package net.foucry.pilldroid; +import android.app.AlarmManager; +import android.app.PendingIntent; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.content.IntentFilter; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v7.app.AlertDialog; @@ -62,11 +66,20 @@ public class MedicamentListActivity extends AppCompatActivity { private View mRecyclerView; private SimpleItemRecyclerViewAdapter mAdapter; + // For the notifications + PendingIntent pendingIntent; + AlarmManager alarmManager; + BroadcastReceiver mReceiver; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_medicament_list); + // Register for alarm + + RegisterAlarmBroadcast(); + dbHelper = new DBHelper(this); dbMedoc = new DBMedoc(this); @@ -158,7 +171,19 @@ public class MedicamentListActivity extends AppCompatActivity { } return super.onOptionsItemSelected(item); } - + + + public void onPause() { + super.onPause(); + + alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 10000, pendingIntent); + } + + protected void onDestroy() { + unregisterReceiver(mReceiver); + super.onDestroy(); + } + public void scanNow(View view) { Intent intent = new Intent("com.google.zxing.client.android.SCAN"); //intent.putExtra("SCAN_MODE", "CODE_128"); @@ -233,6 +258,23 @@ public class MedicamentListActivity extends AppCompatActivity { recyclerView.setAdapter(mAdapter); } + // Received Alarm, display a toast + private void RegisterAlarmBroadcast() { + mReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + Toast.makeText(context, "Vous devez passer à la pharmacie", Toast.LENGTH_LONG).show(); + } + }; + registerReceiver(mReceiver, new IntentFilter("net.foucry.pilldroid")); + pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent("net.foucry.pilldroid"),0); + alarmManager = (AlarmManager)(this.getSystemService(Context.ALARM_SERVICE)); + } + + private void UnregisterAlarmBroadcast(){ + alarmManager.cancel(pendingIntent); + getBaseContext().unregisterReceiver(mReceiver); + } /** * SimpleItemRecyclerViewAdapter */