0

I am making an alarm application in android,which will trigger alarm daily at 8:00 pm.I am using this code to trigger the alarm

     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);

and the service class is

    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();
    }

}

Now the problem is that I am getting same notifications two times a day.I want to fire the notification once in a day but it is not doing like this.The alarm is repeating in wrong interval of time.Can anyone please help me.

4

1 回答 1

0

有一个常数。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);
于 2014-10-24T10:50:56.073 回答