【问题标题】:Alarm Repeats in Wrong Intervals Android警报以错误的间隔重复 Android
【发布时间】:2014-10-24 10:30:12
【问题描述】:

我正在android中制作一个警报应用程序,它将在每天晚上8:00触发警报。我正在使用此代码来触发警报

     Calendar c = Calendar.getInstance();

   c.set(Calendar.HOUR_OF_DAY, 20);
   c.set(Calendar.MINUTE, 0);
   c.set(Calendar.SECOND, 0);
 Intent intentservice = new Intent(MainActivity.this, MyAlarmService.class);

 //create a pending intent to be called at 6 AM
   PendingIntent Pintent = PendingIntent.getService(MainActivity.this, 0, intentservice, PendingIntent.FLAG_UPDATE_CURRENT);
   //schedule time for pending intent, and set the interval to day so that this event will repeat at the selected time every day


   AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(ALARM_SERVICE);

    //schedule time for pending intent, and set the interval to day so that this event will repeat at the selected time every day
  am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 24*3600*1000, Pintent);

服务等级是

    public class MyAlarmService extends Service 
        {

               private NotificationManager mManager;

    @Override
    public IBinder onBind(Intent arg0)
    {
       // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() 
    {
       // TODO Auto-generated method stub  
       super.onCreate();
    }


       @Override
       public void onStart(Intent intent, int startId)
      {
       super.onStart(intent, startId);

       mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
       Intent intent1 = new Intent(this.getApplicationContext(),SecondActivity.class);

       Calendar c = Calendar.getInstance();

       Log.w("**********************","enter ***************** ");


      // Log.w("enter dateeeeeeee"," "+c.get(Calendar.DAY_OF_MONTH));


       int thisDay = c.get(Calendar.DAY_OF_MONTH);

       intent1.putExtra("dateeee", thisDay);

       Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis());
       intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

       PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
       notification.flags |= Notification.FLAG_AUTO_CANCEL;
       notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);

       mManager.cancel(0);

       mManager.notify(0, notification);
    }

    @Override
    public void onDestroy() 
    {
        // TODO Auto-generated method stub
        super.onDestroy();
    }

}

现在的问题是我每天收到两次相同的通知。我想每天触发一次通知,但它不是这样。警报在错误的时间间隔内重复。谁能帮忙我。

【问题讨论】:

  • 年、月、日的值是多少?
  • 写错了。请看我编辑的问题
  • 您是一天连续两次收到通知,还是一天分两次收到通知?
  • 我在一天的不同时间收到相同的通知。

标签: android notifications alarmmanager alarm repeatingalarm


【解决方案1】:

有一个常数。 http://developer.android.com/reference/android/app/AlarmManager.html

替换

am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 24*3600*1000, Pintent);

am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), am.INTERVAL_DAY, Pintent);

【讨论】:

  • 谢谢..我试过了,但又遇到同样的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
  • 1970-01-01
相关资源
最近更新 更多