【问题标题】:Repeating alarm and time changes重复闹钟和时间更改
【发布时间】:2016-04-02 12:34:49
【问题描述】:

我已经在我的应用程序中设置了一个闹钟,该闹钟直到上周都可以正常工作。我使用的代码是这样的

am.setRepeating(AlarmManager.RTC_WAKEUP, setalarmon.getTimeInMillis(), 7000 * 3600 * 24, pi);

它会在当地时间每周五的 11:00 设置闹钟。问题是我的国家因夏令时移至其他时区。我们在 UTC+1 现在我们在 UTC+2,所以警报现在在 12:00 响起。 无论 DST 变化如何设置警报同时触发有什么想法吗?

【问题讨论】:

    标签: android


    【解决方案1】:

    我不知道 setalarmon 到底是什么。我猜它是一个日历对象。问题是你是怎么定义的?

    Calendar setalarmon = Calendar.getInstance(Locale.getDefault());

    应该给你正确的时区。

    【讨论】:

      【解决方案2】:

      是的,它是日历对象并使用正确的本地时间。我的问题是 setRepeating 使用的是 UTC 时间。这就是发生的事情。警报于 3 月 25 日星期五 11:00 正确发射。它应该在 4 月 1 日星期五 11:00 再次触发,因此正确设置为在 7*24*60*60*1000 毫秒后重复。问题是由于 DST 而改变了本地时间,在这些毫秒之后,本地时间是 12 而不是 11,所以警报会在 12 点响起。这就是我想要避免的,我正在寻求帮助

      【讨论】:

        【解决方案3】:

        试试这个:这是我的示例代码

        主活动:

        public static int NOW_NOTIFICATION = 101; 
        
        Intent intent = new Intent(MainActivity.this, NotificationReceiver.class);
                        intent.putExtra(NotificationReceiver.NOTIFICATION_MESSAGE, "Sample Alert");
                        PendingIntent pendingIntent = PendingIntent.getBroadcast(
                                MainActivity.this, NOW_NOTIFICATION, intent, PendingIntent.FLAG_ONE_SHOT);
        
                        cancelNotification(NOW_NOTIFICATION);
                        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                                + (2 * 1000), pendingIntent);
        

        取消通知:

        public void cancelNotification(int requestCode) {
                try {
                    Intent notificationIntent = new Intent(getApplicationContext(), NotificationReceiver.class);
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    AlarmManager alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
                    alarmManager.cancel(pendingIntent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        

        通知接收者:

        public class NotificationReceiver extends BroadcastReceiver {
            public static String NOTIFICATION_MESSAGE = "notificationMessage";
        
        
            @Override
            public void onReceive(Context context, Intent intent) {
                String message = intent.getStringExtra(NOTIFICATION_MESSAGE);
                NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                //Define sound URI
                Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        
                Intent notificationIntent = new Intent(context, EmptyActivity.class);
                notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        
                PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
                final DateTime dateTime = DateTime.now();
                int color = 0xffffaa00;
        //        int color1 = context.getColor(R.color.notificatinBackgroundColor);
                NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
                builder.setSmallIcon(R.drawable.festi_push_message_small);
                builder.setContentIntent(pendingIntent);
                builder.setContentTitle("Notification Sample");
                builder.setAutoCancel(true);
                builder.setContentText(message + ", Current Time : " + dateTime.getHourOfDay() + ":" + dateTime.getMinuteOfHour() + ":" + dateTime.getSecondOfMinute());
                builder.setSound(soundUri);
                builder.setLights(Color.RED, 1000, 1000);
                builder.setColor(color);
        
        
                Notification notification = builder.build();
        
                notification.flags |= Notification.FLAG_AUTO_CANCEL;
        
                notificationManager.notify(102938, notification);
            }
        
        }
        

        TimeZoneChangedReceiver:

        public class TimeZoneChangedReceiver extends BroadcastReceiver {
            public static String NOTIFICATION_MESSAGE = "notificationMessage";
        
            public static int NOW_NOTIFICATION = 101;
        
            Context context;
        
            @Override
            public void onReceive(Context context, Intent intent) {
                this.context = context;
                Intent intentNitification = new Intent(context, NotificationReceiver.class);
                intentNitification.putExtra(NotificationReceiver.NOTIFICATION_MESSAGE, "Sample Alert");
                PendingIntent pendingIntent = PendingIntent.getBroadcast(
                        context, NOW_NOTIFICATION, intentNitification, PendingIntent.FLAG_ONE_SHOT);
        
                cancelNotification(NOW_NOTIFICATION);
                AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
                alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                        + (2 * 1000), pendingIntent);
            }
        
            public void cancelNotification(int requestCode) {
                try {
                    Intent notificationIntent = new Intent(context, NotificationReceiver.class);
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                    alarmManager.cancel(pendingIntent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        

        AndroidManifest:

        <receiver android:name=".NotificationReceiver" />
        
                <receiver android:name=".TimeZoneChangedReceiver">
                    <intent-filter>
                        <action android:name="android.intent.action.TIMEZONE_CHANGED" />
                    </intent-filter>
                </receiver>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-06-10
          相关资源
          最近更新 更多