Java Android计时器在指定时间之前执行

Java Android计时器在指定时间之前执行,java,android,android-notifications,Java,Android,Android Notifications,我使用一个代码根据特定条件每小时向用户显示一个通知。我正在学习,所以最基本的代码在这里 Log.v("Service Started", "onStartCommand"); Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { Log.v("Show Noti

我使用一个代码根据特定条件每小时向用户显示一个通知。我正在学习,所以最基本的代码在这里

Log.v("Service Started", "onStartCommand");
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Log.v("Show Notification", "Showing notification at "+System.currentTimeMillis());
                NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

                long[] pattern = {500,500,500,500,500,500,500,500,500};
                Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

                Intent intent1 = new Intent(getBaseContext(), SplashScreen.class);
                PendingIntent pendingIntent = PendingIntent.getActivity(getBaseContext(),123,intent1,0);
                Notification notification = new NotificationCompat.Builder(getBaseContext())
                        .setContentTitle("Dimensions Weather & News")
                        .setContentText("Thank you for using our app.")
                        .setAutoCancel(true)
                        .setContentIntent(pendingIntent)
                        .setLights(Color.BLUE, 500, 500)
                        .setVibrate(pattern)
                        .setSound(alarmSound)
                        .setSmallIcon(R.drawable.ic_launcher).getNotification();

                notification.flags = Notification.FLAG_AUTO_CANCEL;
                notificationManager.notify(123, notification);
            }
        }, 0, 3600000);

但它会时不时地显示通知,而不是每隔一小时显示一次。大多数情况下,它显示在前一个6-7分钟后。为什么?

时间任务取决于系统时间,请使用AlarmManager实例化它。

即使它确实取决于系统时间,我也不会更改它。3600000毫秒应每小时确认一次通知。不要在android应用程序中使用定时器/定时器任务,而是使用AlarmManager