从片段开始报警

Starting alarm from fragment

我想从片段启动 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);

如果您将闹钟设置为在设置闹钟后的当前时间响铃,闹钟如何响起。尝试将其设置为将来的某个时间

这样做

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

这就是问题所在:

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