In my application I would like to set an AlarmManager to a specific time: 2016-11-15 13:00:00
. Then show a notification (or toast) without repeating it and then turn off. But my code doesn't work.
MainActivity:
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.clear();
cal.set(2016, 11, 15, 13, 00);
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
AlarmReceiver:
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Utils.toast("dddddd", context);
Log.e("alarm!!!!!! ", " fire");
}
}
manifest:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<receiver android:name=".Views.AlarmReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
What is the problem with this code, why does the alarm manager
not show a toast
or Log
?