【问题标题】:How do I schedule an alarm for particular days in a week on Android?如何在 Android 上为一周中的特定日子安排闹钟?
【发布时间】:2019-02-14 19:04:54
【问题描述】:

我有这个方法。如果我在一天之内设置闹钟,它工作正常。我需要在 Android 上为一周/月/年的特定日子安排闹钟。

public static void setAlarmFragment(int intervalDays, Context context, Calendar targetCal) {

    if (targetCal != null) {

        Intent intent = new Intent(context, PrescriptionAlarmActivity.class);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        int uniqueId = CommonUtils.generateRandomID();
        intent.putExtra("uniqueId", uniqueId);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, uniqueId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        int i = Build.VERSION.SDK_INT;
        if (alarmManager != null) {
            if (intervalDays > 0) {
                long customAlarmInterval = AlarmManager.INTERVAL_DAY * intervalDays;
                alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
                        targetCal.getTimeInMillis(), customAlarmInterval, pendingIntent);
                Log.e("AlarmBroadcastReceiver", "Alarm set: " + CommonUtils.getAlarmTime(targetCal));
            }
            else {
                if (i < 19) {
                    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
                }
                else if (i < 23) {
                    alarmManager.setExact(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
                }
                else {
                    alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
                }
                Log.e("AlarmBroadcastReceiver", "Alarm set: " + CommonUtils.getAlarmTime(targetCal));
            }
        }
    }
}

【问题讨论】:

    标签: android scheduled-tasks alarm


    【解决方案1】:

    你必须:

    1. 使用约束布局。

    2. 避免硬编码布局大小。

    为确保您的布局灵活并适应不同的屏幕尺寸,您应该对大多数视图组件的宽度和高度使用“wrap_content”和“match_parent”,而不是硬编码的尺寸。

    “wrap_content”告诉视图将其大小设置为适合该视图中的内容所需的大小。

    "match_parent" 使视图尽可能地扩展到父视图内。

    【讨论】:

      【解决方案2】:

      每个设备都可以使用一个设计正确的布局,但它可能看起来很奇怪。

      正确的做法是首先设计一个横向布局,并在不同的设备上进行测试,例如平板电脑和低密度智能手机。

      然后选择更具体的分辨率,例如纵向 7 英寸平板电脑和横向 10.1 英寸平板电脑。

      到这里你肯定已经实现了几乎所有你需要的屏幕。

      如果某项功能在智能手机上运行良好而在另一台智能手机上表现不佳,则必须检查邮件布局 XML 的设计方式。

      无论如何,如果您需要针对低分辨率设备进行更具体的设计,您可以寻找 hdpi 分辨率。

      【讨论】:

        【解决方案3】:

        您需要在res文件夹中添加一个dimens.xml文件:

        【讨论】:

          猜你喜欢
          • 2014-04-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-09
          • 2016-09-25
          相关资源
          最近更新 更多