Android将AlarmManager设置为显示特定日期和时间的通知

时间:2016-11-15 09:29:51

标签: android alarmmanager

在我的应用程序中,我想将AlarmManager设置为特定时间:2016-11-15 13:00:00。然后显示通知(或吐司)而不重复它然后关闭。但我的代码不起作用。

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");
    }
}

清单

<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>

此代码有什么问题,为什么alarm manager没有显示toastLog

0 个答案:

没有答案