passage d'un parcelable entre activité via un BroadcastReceiver
bonjour,
J'ai une activité qui passe un objet Alarm
Code:
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
|
import android.os.Parcel;
import android.os.Parcelable;
import android.text.format.Time;
import android.util.Log;
public class Alarm implements Parcelable
{
private Time heure;
private boolean active;
private int Rappel;
public Alarm(Parcel source) {
// TODO Auto-generated constructor stub
heure.set(source.readLong());
this.active = (source.readInt() == 1);
this.Rappel = source.readInt();
}
public Alarm() {
// TODO Auto-generated constructor stub
}
public Time getHeure() {
return heure;
}
public void setHeure(Time heure) {
this.heure = heure;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
/**
* @return the rappel
*/
public int getRappel() {
return Rappel;
}
/**
* @param rappel the rappel to set
*/
public void setRappel(int rappel) {
Rappel = rappel;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags)
{
Log.i("Alarm","*************** "+heure.toMillis(true)+" *************");
dest.writeLong(heure.toMillis(true));
dest.writeInt(active ? 1 :0);
dest.writeInt(Rappel);
}
public static Parcelable.Creator<Alarm> getCreator()
{
return CREATOR;
}
public static final Parcelable.Creator<Alarm> CREATOR = new Parcelable.Creator<Alarm>()
{
@Override
public Alarm createFromParcel(Parcel source)
{
return new Alarm(source);
}
@Override
public Alarm[] newArray(int size)
{
return new Alarm[size];
}
}; |
comme ceci :
Code:
1 2 3 4 5 6 7 8 9 10 11
|
private void planifierAlarm() {
//Récupération de l'instance du service AlarmManager.
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//On instancie l'Intent qui va être appelé au moment du reveil.
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("Alarm", alarm);
//On créer le pending Intent qui identifie l'Intent de reveil avec un ID et un/des flag(s)
PendingIntent pendingintent = PendingIntent.getBroadcast(this, ALARM_ID, intent, 0); |
Dans le Receiver je le recupère comme cela :
Code:
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
|
public class AlarmReceiver extends BroadcastReceiver{
public static final int ID_NOTIFICATION = 1963;
@Override
public void onReceive(Context context, Intent intent) {
try {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Alarm alarm = null;
// 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.Seballader,
"notification SeBallader !", System.currentTimeMillis());
//final byte[] data = intent.getByteArrayExtra("Alarm");
final Bundle data=intent.getExtras();
Intent defineIntent = new Intent(context, Replannif.class);
defineIntent.putExtra("Alarm", data);
// putByteArrayExtra("Alarm", alarm);
final PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, defineIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// On définit le titre de la notif
String titreNotification = "Ok ????!";
// On définit le texte qui caractérise la notif
String texteNotification = "cliquer pour confirmer ou infirmer votre Arrivé";
// On configure notre notification avec tous les paramètres que l'on
// vient de créer
notification.setLatestEventInfo(context, 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
notification.vibrate = new long[] { 0, 200, 100, 200, 100, 200 };
notification.flags|=Notification.FLAG_AUTO_CANCEL;
// Enfin on ajoute notre notification et son ID à notre gestionnaire de
// notification
notificationManager.notify(ID_NOTIFICATION, notification);
}
catch (Exception r) {
Toast.makeText(context, "Erreur.",Toast.LENGTH_SHORT).show();
r.printStackTrace();
}
} |
je récupère le parcelable dans l'activité Replannif.class comme ceci :
Code:
1 2 3 4
|
//Mise en place de L'alarm
Alarm alarm = getIntent().getExtras().getParcelable("Alarm"); |
j'ai l'erreur suivante :
Code:
1 2 3 4
|
02-26 19:34:03.260: E/AndroidRuntime(496): java.lang.RuntimeException:
Unable to start activity ComponentInfo{com.dinasoft.Seballader/com.dinasoft.Seballader.chemin.Replannif}:
java.lang.ClassCastException: android.os.Bundle |
Comment je dois faire car je pense que dans le AlarmReceiver ne ne dois pas le repasser en Bundle ?
Je ne sais pas du tout comment faire
De plus j'ai en doute sur le faite de passer un Time dans Parcelable en Long
merci de vos réponses