首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >警报以错误的时间间隔重复

警报以错误的时间间隔重复
EN

Stack Overflow用户
提问于 2014-10-24 18:30:12
回答 1查看 328关注 0票数 0

我正在用android做一个闹钟应用程序,它会在每天晚上8:00触发闹钟。我正在使用这个代码来触发闹钟

代码语言:javascript
复制
     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);

服务级别是

代码语言:javascript
复制
    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();
    }

}

现在的问题是,我每天收到两次相同的通知,我想每天发出一次通知,但它不会像this.The警报在time.Can的错误间隔中重复,任何人请帮助我。

EN

回答 1

Stack Overflow用户

发布于 2014-10-24 18:50:56

这是一个常量。http://developer.android.com/reference/android/app/AlarmManager.html

替换

代码语言:javascript
复制
am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 24*3600*1000, Pintent);

使用

代码语言:javascript
复制
am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), am.INTERVAL_DAY, Pintent);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26546192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档