Problème avec AlarmManager
Bonjour,
Je suis au début de mon auto-formation sur Android. Je suis en train de faire le célèbre tutoriel du réveil matin :)
Cependant je rencontre un problème, mon alarme ne se déclenche pas et le BroadcastReceiver n'est jamais appelé.
J'ai retourné mon code dans tous les sens je ne vois pas ce qui cloche, je pense qu'un regard nouveau serait le bienvenue. Merci d'avance pour votre aide :)
Mon Code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
//Récupération de l'instance du service AlarmManager.
AlarmManager amAlarmMG = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
//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);
//On annule l'alarm pour replanifier si besoin
amAlarmMG.cancel(pendingintent);
//on prépare l'alarme de test
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.add(Calendar.SECOND, 5);
amAlarmMG.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingintent ); |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
public class AlarmReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG,"Notification d'alarme reçu!");
// TODO Auto-generated method stub
try
{
Toast.makeText(context, "C'est l'heure!", Toast.LENGTH_LONG).show();
}
catch( Exception e)
{
Toast.makeText(context, "Erreur!", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
} |
Mon Manifest :
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
|
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.baptistel.reveilmatin"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ReveilMatin"
android:label="@string/app_name" android:theme="@android:style/Theme.Light">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.baptistel.reveilmatin.AlarmReceiver" android:process=":remote" />
<receiver android:name="com.baptistel.reveilmatin.AlarmBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
</manifest> |