1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
package Android.App;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import Android.App.R;
import Android.App.Traitement.GlobalContextTraitement;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.MediaStore.Audio;
import android.util.Log;
public class MyPharmacyBackgroundService extends Service {
private Timer timer ;
public static int ID_NOTIFICATION = 1988;
@Override
public void onCreate() {
//android.os.Debug.waitForDebugger();
super.onCreate();
timer = new Timer();
Log.d(this.getClass().getName(), "onCreate");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(this.getClass().getName(), "onStart");
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
// Executer de votre tâche
Log.d("DDD", "Fisrt");
List<Entity.NotificationInfo> infos=new NotificationClass(getApplicationContext()).GetNotifications();
if (infos ==null)
return;
Log.d("DDD",String.valueOf(infos.size()));
for(Entity.NotificationInfo info:infos)
createNotify(info);
}
}, 0, 60000); //3600000
return START_NOT_STICKY;
// return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
Log.d(this.getClass().getName(), "onDestroy");
this.timer.cancel();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
//Méthode qui créer la notification
private void createNotify(Entity.NotificationInfo info){
//On créer un "gestionnaire de notification"
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//On créer la notification
//Avec son icône et son texte défilant (optionel si l'on veut pas de texte défilant on met cet argument à null)
Notification notification = new Notification(R.drawable.traitement, "MyPH@RM@CY : Vous avez un traitement a prendre !", System.currentTimeMillis());
//Le PendingIntent c'est ce qui va nous permettre d'atteindre notre deuxième Activity
//ActivityNotification sera donc le nom de notre seconde Activity
Intent intent= new Intent(GlobalContextTraitement.context, NotificationActivity.class);
Bundle bundle = new Bundle();
bundle.putString("notificaion", info.GetSerialeValue());
bundle.putInt("NotifictionID",ID_NOTIFICATION);
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getActivity(GlobalContextTraitement.context, 0, intent, 0);
//On définit le titre de la notif
String titreNotification = "MyPH@RM@CY Notification !";
//On définit le texte qui caractérise la notif
String texteNotification = "Vous avez un medicament a prendre...";
//On configure notre notification avec tous les paramètres que l'on vient de créer
notification.setLatestEventInfo(this, titreNotification, texteNotification, pendingIntent);
//On ajoute un style de vibration à notre notification
//L'utilisateur est donc également averti par les vibrations de son téléphone
//Ici les chiffres correspondent à 0sec de pause, 0.2sec de vibration, 0.1sec de pause, 0.2sec de vibration, 0.1sec de pause, 0.2sec de vibration
//Vous pouvez bien entendu modifier ces valeurs à votre convenance
if (info.isIsVibreur())
notification.vibrate = new long[] {0,200,100,200,100,200};
if (info.isIsSonnerie())
notification.sound=Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URI, "2");
//Enfin on ajoute notre notification et son ID à notre gestionnaire de notification
notificationManager.notify(ID_NOTIFICATION, notification);
// ID_NOTIFICATION++; pour avoir plusieurs notification veuillez increment ID_NOTIFICATION
}
//Méthode pour supprimer de la liste de notification la notification que l'on vient de créer
} |
Partager