I am doing a time table project for my college, in which I want to generate notification on each hour using AlarmManager, I almost got the solution, but the alarm gets triggered during random time, like between hours. What I want is to trigger alarm manger messages on exactly each hour, like 7.00am,8.00am, and so on on everyday. My code is given below.
MainActivity.java
public class MainActivity extends AppCompatActivity {
DatabaseHelper2 db = new DatabaseHelper2(this);
private PendingIntent pendingIntent;
private AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Cursor resService = db.getAllData();
int pref = 0;
if(resService.getCount()!=0){
Date date2 = new Date();
int h=date2.getHours();
int m = date2.getMinutes();
int s = date2.getSeconds();
Intent alarmIntent = new Intent(this,AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this,0,alarmIntent,0);
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
//int interval = 60 * 60 * 10000;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),AlarmManager.INTERVAL_HOUR,pendingIntent);
Toast.makeText(this,"Alarm set at "+h+" : "+m+" : "+s,Toast.LENGTH_LONG).show();
}
}
AlarmReciever.java
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Date d = new Date();
Toast.makeText(context,"Broadcast message triggered at "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds() ,Toast.LENGTH_SHORT).show();
Intent service1 = new Intent(context, MyService.class);
context.startService(service1);
}
}
MyService.java
public class MyService extends Service {
DatabaseHelper2 db = new DatabaseHelper2(this);
String sub;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Date date = new Date();
int now=date.getHours();
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
String dayOfWeek = sdf.format(date);
Cursor resNow = db.getNowSub(now,dayOfWeek);
if(resNow.getCount()!=0){
if(resNow.moveToNext()){
sub = resNow.getString(3);
}
NotificationManager notif = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notify = new Notification.Builder
(getApplicationContext()).setContentTitle("NowSub").setContentText("Now:"+sub+ " at "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds()).setContentTitle("currenet subject").setSmallIcon(R.mipmap.ic_launcher).build();
notify.flags |= Notification.FLAG_AUTO_CANCEL;
notif.notify(0, notify);
}
return START_STICKY;
}
I spent more than a week for this particular problem, hope someone will help. Thanks in advance.