last day (112 days later) » 

10:56 AM
hi aradhna
 
Hey!
Thanks for coming here
So let me start by explaining what I want to do in my app
 
no matter, several people helped me in my initial days
 
There are particular dates on which the app has to send a notification. The dates are stored locally in an SQLite database.
 
ok please proceed
 
What i want my app to do is this- everyday at 8 in the morning, it will check the database to see weather it has to send the notification or not. If the database has an entry for today, then the app will send a notification to the user.
I have my database ready and also know how to send the notification.
What I am stuck at, is how to implement the part where the app checks the database at 8 everyday
 
11:04 AM
ok so you are done with most of the part now you have to implement those all code of db and notification into your newly created service class
 
yes
what i want to understand is if i should make a service or create an alarammanager that calls a pending intent which in case checks the db
 
adjactly
you have to use calender class to check 8 am like Calendar aradhna = Calendar.getInstance();
aradhna.set(Calendar.HOUR, 8); // At the hour you wanna fire
aradhna.set(Calendar.MINUTE, 0); // Particular minute
aradhna.set(Calendar.SECOND, 0); // particular second
aradhna.set(Calendar.HOUR, 8); // At the hour you wanna fire
 
i know the code to give time to alarmmanager
but what i dont know is that once the alarm is called, do i need to initialise it agian or not?
 
for sure, if you unless you are in a loop
sorry, for sure, unless you are in a loop
 
so...
 
11:13 AM
makes sense ?
 
what i can do is create something like this
 
ok just use alarmmanager.set() and inside that use pending Intent which in turn will include your db .class file with notification
 
Intent intent = new Intent(Splash.this, AlarmService.class);
PendingIntent mAlarmSender = PendingIntent.getService(AlarmSettings.this, Id, intent, 0);

AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
can you tell me how to write the am.set() line?
i am not sure about what parameters i should provide for the set() function
 
wait wait, first extract the system current time then match both your calender time and system time in IF block
long intendedTime = YOUR TIME.getTimeInMillis();
long currentTime = current.getTimeInMillis();
get it in long variable then ... if(YOUR TIME == currentTime){

alarmManager.setRepeating();
}
Step 1- create a actvity having db & notification code Step 2- create a service class step 3- code to set your 8 am & get current time
 
i have step 1 done
lets start with step 2
 
11:23 AM
step 3 - coding will be in service class
 
i mean, can you guide me through step 2
like what my service class should be like
 
just create the service class first by extending service
 
yea, yea that i know
but what should i proovide in onStartCommand
 
you should code for Intent pointing to db activity
pendingItent and alarmmanager with timestamp code
 
its getting confusing now
 
11:28 AM
wait inside onStartcommand() write
Intent myIntent = new Intent(Splash.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(Splash.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

Calendar firingCal= Calendar.getInstance();
Calendar currentCal = Calendar.getInstance();

firingCal.set(Calendar.HOUR, 8); // At the hour you wanna fire
firingCal.set(Calendar.MINUTE, 0); // Particular minute
firingCal.set(Calendar.SECOND, 0); // particular second

long intendedTime = firingCal.getTimeInMillis();
 
currently my service class looks like this
public class MyService extends Service{

SharedPreferences myPrefs;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub


return super.onStartCommand(intent, flags, startId);
}
 
pleaes improve my posted code
also retrun return START_STICKY; in onstartcommand()
 
ok, so when intendedTime>-currentTime, the alarm will be set
ok, another question, this code will everyday at 8 exactly and will set a new alarm everyday, right?
 
no no you have to check with 8 am calender obj, with your time == current time in if block
when system time will be 8 condition will be true and your db activity will be called
 
yea so when the phone's time is 8, it will open up my intent which i provided with the alarm and set a new alarm for the next day, right?
 
11:32 AM
also i think no need of alarm manager
just we can play with calender class and if check to call db activity intent
 
yea, so in my service class, i can check if time is 8 i can directly call the db and do my work
and this code will run everyday at 8
right?
 
yoh, u got it
 
one last question, i just need to declare my service in manifest and that's it? i dont have to start it from anywhere in the code?
 
ok plese wait
if you start your service in START STICKY MODE , I suggested above , it will run continuisly
ok now you have to register it in manifest
 
the last time i used services, i registered it in manifest and then called it from my main activity.
so in this i just have to register my service in manifest and use start sticky_mode in onStartCommand and that's it?
i dont have to call my service class from anywhere in the code?
 
11:37 AM
and after manifest you have to call from splash or first activity like this
Intent intent = new Intent(this, NotificationService.class);
startService(intent);
 
yes that is what i was asking
but then everytime the app is opened, it will call the service
how to stop that?
 
dont forget to use aprrprate permission in manifest
no matter condition inside service will not be satisfied and no notification will be send
 
should i do it like this,
step 1- check if app is opened for the first time
if yes, only then call the service
 
please set time appropriately with AM or PM diff
 
otherwise i dont call the service since it will be running
 
11:40 AM
no need as i think
but you can do aradhna
 
i am confused
if a service is already running, what will happen when i start it again?
 
nothing just condition will be checked again and again,
 
ok, thanks
 
thanks aarohi
sorry aradhna, my time up now
 
thanks so much for helping me out
i am gonna let this group be here so that when i have problems in future i can reach you from here
you can also text here when ever you want :)
 
11:45 AM
@aradhna First upvote the answer, to increase my point, if you think it was fruitfull to you. I request — Akash kumar 56 mins ago
 
I think you understood and you will achieve the right output
 
delete this please @Akashkumar :p
and do update your answer with relevant code. you will get more upvotes if people get help from the answer in future.
 
yah sure
 
cool
cya
 
what is cya
ok bye everyone
 

  last day (112 days later) »