我们如何检查是否存在警报/是否在我们的Android手机/我的应用中创建了警报

时间:2019-03-29 05:14:17

标签: android

我已经构建了一个小提醒应用程序演示。现在的问题有时是提醒到来还是有时没有?现在,我具有此功能,用户可以将警报设置为日,月,周,年。

现在如何检查通知即将到来的月份或年份?

很明显,当我手动转到并更改Android日期和时间设置时,不会响起警报。 (甚至还有几分钟)

请参阅:When I change Android phone date and time, notification not getting, otherwise, if I do nothing, I get notification on time

修改: 代码:

public void setRepeatAlarm(Context context, Calendar calendar, int ID, long RepeatTime, int requestCode) {
        mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        // Put Reminder ID in Intent Extra
        Intent intent = new Intent(context, AlarmReceiver.class);
        intent.putExtra(REMINDER_ID, Integer.toString(ID));


        mPendingIntent = PendingIntent.getBroadcast(context, ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        mPendingIntent = PendingIntent.getBroadcast(context, requestCode, new Intent(context, AlarmReceiver.class),
                PendingIntent.FLAG_NO_CREATE);
         isAlarmSet = (mPendingIntent != null);
         if (isAlarmSet) {
             showLog("isAlarmSet: " + isAlarmSet);
         } else {
             showLog("isAlarmSet: " + isAlarmSet);
         }
        // Calculate notification timein
        Calendar c = Calendar.getInstance();
        long currentTime = c.getTimeInMillis();
        long diffTime = calendar.getTimeInMillis() - currentTime;

        // Start alarm using initial notification time and repeat interval time
        mAlarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME,
                SystemClock.elapsedRealtime() + diffTime,
                RepeatTime, mPendingIntent);

        // Restart alarm if device is rebooted
        ComponentName receiver = new ComponentName(context, BootReceiver.class);
        PackageManager pm = context.getPackageManager();
        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    }

3 个答案:

答案 0 :(得分:0)

您可以使用以下方法检查现有警报:

PendingIntent pendingIntent = PendingIntent.getBroadcast(this, requestCode,
           new Intent(this, AlarmReceiver.class),
           PendingIntent.FLAG_NO_CREATE);
boolean isAlarmSet = (pendingIntent!=null);

请注意,此处的 requestCode 应该与设置警报时使用的相同。另外, AlarmReceiver 是您的广播广播类


在您提到的情况(更改电话时间)中,我还没有对此进行测试,但是您可以参考this并根据新时间再次设置闹钟,并取消上一个时间。

答案 1 :(得分:0)

您对设计模式非常了解。请遵循观察者模式来管理以下内容,无论是否手动更改时间,都应始终获取新时间。它将检查当前时间是否更改。更多详细信息,请阅读观察者模式文档。

// Calculate notification timein
        Calendar c = Calendar.getInstance();
        long currentTime = c.getTimeInMillis();
        long diffTime = calendar.getTimeInMillis() - currentTime; 

答案 2 :(得分:0)

您应该听android系统中的时间变化并采取相应的行动

<receiver android:name=".TimeChangedReceiver">
    <intent-filter >
        <action android:name="android.intent.action.TIME_SET"/>
    </intent-filter>
</receiver>

广播接收器类

public class TimeChangedReceiver extends BroadcastReceiver{

@Override
public void onReceive(final Context arg0, Intent arg1) {
//Call your Alarm setting code
 // change your alarm accordingly
    }
}