Désactiver Alarme une fois l'heure programmée passée
Bonjour à tous,
Dans mon application, je souhaite porgrammer une notification à une date précise. Voici donc le code que j'utilise, d'après un tutorial que j'ai suivi:
AlarmReceiver.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// When our Alaram time is triggered , this method will be excuted (onReceive)
// We're invoking a service in this method which shows Notification to the User
Intent myIntent = new Intent(context, NotificationService.class);
context.startService(myIntent);
}
} |
NotificationService.java
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
| import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class NotificationService extends Service {
private NotificationManager mManager;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
// Getting Notification Service
mManager = (NotificationManager) this.getApplicationContext()
.getSystemService(
this.getApplicationContext().NOTIFICATION_SERVICE);
/*
* When the user taps the notification we have to show the Home Screen
* of our App, this job can be done with the help of the following
* Intent.
*/
Intent intent1 = new Intent(this.getApplicationContext(), MyView.class);
Notification notification = new Notification(R.drawable.ic_launcher,
"Message de la notification", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
this.getApplicationContext(), 0, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this.getApplicationContext(),
"Message", "Message de la notification",
pendingNotificationIntent);
mManager.notify(0, notification);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
} |
MyView.java (Activité principale)
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
| import java.util.Calendar;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
public class MyView extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Calendar Calendar_Object = Calendar.getInstance();
Calendar_Object.set(Calendar.MONTH, 4);
Calendar_Object.set(Calendar.YEAR, 2013);
Calendar_Object.set(Calendar.DAY_OF_MONTH, 8);
Calendar_Object.set(Calendar.HOUR_OF_DAY, 12);
Calendar_Object.set(Calendar.MINUTE, 02);
Calendar_Object.set(Calendar.SECOND, 0);
// MyView is my current Activity, and AlarmReceiver is the
// BoradCastReceiver
Intent myIntent = new Intent(MyView.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MyView.this,
0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
/*
* The following sets the Alarm in the specific time by getting the long
* value of the alarm date time which is in calendar object by calling
* the getTimeInMillis(). Since Alarm supports only long value , we're
* using this method.
*/
alarmManager.set(AlarmManager.RTC, Calendar_Object.getTimeInMillis(),
pendingIntent);
} |
Et l'ajout de ceci dans mon manifest :
Code:
1 2 3 4 5 6
| <receiver android:name=".AlarmReceiver" >
</receiver>
<service
android:name="NotificationService"
android:enabled="true" /> |
La notification se lance bien à la date et l'heure prévue. En revanche, une fois que l'heure est passée, lorsque je relance mon application, la notification se lance à nouveau.
Comme puis-je faire alors pour que la notification se désactive dès que l'heure que j'ai programmé est passée?
Je vous remercie par avance pour votre aide ;)