Hello,

I have a problem. I made an app just to notify me in a point of time, but it notifies me sometimes on time, and sometimes it's late for few seconds... Why is that so? Here is my code...

MainActivity.java

package beslimir.notificationdaily;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       Calendar calendar = Calendar.getInstance();
       calendar.set(Calendar.HOUR_OF_DAY, 15);
       calendar.set(Calendar.MINUTE, 43);
       calendar.set(Calendar.SECOND, 30);

       Log.i("calendar", calendar.toString());

       Intent intent1 = new Intent(MainActivity.this, AlarmReceiver.class);
       PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);

       AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
       am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

       Log.i("alarm", AlarmManager.RTC_WAKEUP + " " + calendar.getTime());
   }
}


and my AlarmReceiver.java

package beslimir.notificationdaily;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;

/**
* Created by beslimir on 28.11.16..
*/

public class AlarmReceiver extends BroadcastReceiver {

   @Override
   public void onReceive(Context context, Intent intent) {
       // TODO Auto-generated method stub

       long when = System.currentTimeMillis();
       int counter = 0;
       NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

       Intent notificationIntent = new Intent(context, ResultActivity.class);
       notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

       PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

       Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

       NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context)
               .setSmallIcon(R.mipmap.ic_launcher)
               .setContentTitle("Executed")
               .setContentText("SubTitle").setSound(alarmSound)
               .setAutoCancel(true).setWhen(when)
               .setContentIntent(pendingIntent)
               .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
       notificationManager.notify(counter, mNotifyBuilder.build());
       counter++;
   }

}

Hope someone can help me :)

Thanks! ;)