从片段开始报警

时间:2015-01-06 16:27:08

标签: android fragment alarmmanager

我想从片段启动alarmmanager,但不能。这段代码在活动中非常完美,但在片段中没有。有帮助吗? 这是我的片段类的代码:

        hour = tp1.getCurrentHour();
        minute = tp1.getCurrentMinute();

        cal.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis());
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, minute);
        cal.set(Calendar.SECOND, 00);

        Intent intent = new Intent(getActivity(), AlarmReceiverActivity.class);

        PendingIntent pendint = PendingIntent.getActivity(getActivity(), 2, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        AlarmManager am = (AlarmManager) getActivity().getSystemService(getActivity().ALARM_SERVICE);

        am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendint);

2 个答案:

答案 0 :(得分:0)

如果您将闹钟设置为在设置闹钟后已经过的当前时间关闭,闹钟怎么会关闭。尝试将其设置为将来的时间

这样做

cal.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, hour);
cal.set(Calendar.MINUTE, minute);
cal.set(Calendar.SECOND, 00);
cal.add(Calendar.SECOND, 65);

答案 1 :(得分:0)

这就是问题:

<activity android:name="AlarmReceiverActivity"></activity>
<activity android:name="RepeatingAlarmReceiverActivity"></activity>

我改变了:

<activity android:name="com.indyvision.test.tabswipe.fragments.AlarmReceiverActivity"></activity>
<activity android:name="com.indyvision.test.tabswipe.fragments.RepeatingAlarmReceiverActivity"></activity>