Android execute a function after 1 hour - android

I have an android application where I am storing user's data on database when he/she activates the app. My app requires the user to stop the application manually in order to remove its entry from the database and along with that other services which keep running when the app is activated.
So I want to write a function which will be executed after every hour (when the app is activated) and will give a notification to user just to remind him/her about the service which is running .If the user had forgot to stop the service then they can stop it or continue with service.
What is the best efficient way of doing this. I dont want to drain too much of battery with thihs 1 hour basis check if the user considers it to run for a day or so. Please advice. Thanks :)

I suggest the code will be like this.
// the scheduler
protected FunctionEveryHour scheduler;
// method to schedule your actions
private void scheduleEveryOneHour(){
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
new Intent(WAKE_UP_AFTER_ONE_HOUR),
PendingIntent.FLAG_UPDATE_CURRENT);
// wake up time every 1 hour
Calendar wakeUpTime = Calendar.getInstance();
wakeUpTime.add(Calendar.SECOND, 60 * 60);
AlarmManager aMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
aMgr.set(AlarmManager.RTC_WAKEUP,
wakeUpTime.getTimeInMillis(),
pendingIntent);
}
//put this in the creation of service or if service is running long operations put this in onStartCommand
scheduler = new FunctionEveryHour();
registerReceiver(scheduler , new IntentFilter(WAKE_UP_AFTER_ONE_HOUR));
// broadcastreceiver to handle your work
class FunctionEveryHour extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// if phone is lock use PowerManager to acquire lock
// your code to handle operations every one hour...
// after that call again your method to schedule again
// if you have boolean if the user doesnt want to continue
// create a Preference or store it and retrieve it here like
boolean mContinue = getUserPreference(USER_CONTINUE_OR_NOT);//
if(mContinue){
scheduleEveryOneHour();
}
}
}
hope that helps :)

Use AlarmManager refer this and tutorial with PendingIntent

Try this way,hope this will help you to solve your problem.
new Timer().scheduleAtFixedRate(task, after, interval);

Related

How to add adding "remind me after half an hour" functionality - android

I have done an application that fire an alarm in certain time, and i am stuck on implementing remind me after half an hour functionality
what can i do to implement receiver, or service or anything that runs after half an hour of clicking the button of reming me after half an hour
any suggestions ?
Edited the code from Android execute a function after 1 hour to half an hour.
// the scheduler
protected FunctionEveryHalfHour scheduler;
// method to schedule your actions
private void scheduleEveryHalfHour(){
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
new Intent(WAKE_UP_AFTER_HALF_HOUR),
PendingIntent.FLAG_UPDATE_CURRENT);
// wake up time every 1 hour
Calendar wakeUpTime = Calendar.getInstance();
wakeUpTime.add(Calendar.SECOND, 30 * 60);
AlarmManager aMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
aMgr.set(AlarmManager.RTC_WAKEUP,
wakeUpTime.getTimeInMillis(),
pendingIntent);
}
//put this in the creation of service or if service is running long operations put this in onStartCommand
scheduler = new FunctionEveryHalfHour();
registerReceiver(scheduler , new IntentFilter(WAKE_UP_AFTER_HALF_HOUR));
// broadcastreceiver to handle your work
class FunctionEveryHalfHour extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// if phone is lock use PowerManager to acquire lock
// your code to handle operations every half hour...
// after that call again your method to schedule again
// if you have boolean if the user doesnt want to continue
// create a Preference or store it and retrieve it here like
boolean mContinue = getUserPreference(USER_CONTINUE_OR_NOT);//
if(mContinue){
scheduleEveryHalfHour();
}
}
}
You can write a simple service with a timer and whenever the time is up.it can do your thing.all you need to do is start a service with a timer inside of it

General regarding service class and multithreading

I am running a code, where the user selects a date and time. The user can select any date and time in the future. These dates and time are stored in sqlite database. After the user selects those dates and time, the activity calls a service class, where I am running a new thread in the following way
public int onStartCommand(Intent intent, int flags, int startId) {
final AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
final SqliteController db = new SqliteController(getApplicationContext());
new Thread(new Runnable() {
#Override
public void run() {
List<Greetings> greetings = db.getAllGreetings();
if (db.getGreetingsCount() >= 0) {
do {
for (Greetings g : greetings) {
Calendar cal = Calendar.getInstance();
.........
.........//other codes
This thread access the data from the database and matches the time and date with the system time and date. One the date and time matches, I am using alarm manager with broadcast receiver like this
if (dnt.equals(cdt)) {
Intent aint = new Intent(getApplicationContext(), AlarmReceiver.class);
aint.putExtra("msg", msg);
aint.putExtra("phone", phone);
aint.putExtra("id", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, aint, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
db.deleteGreetings(g);
}
I wanted to know, Is this the correct way to do it? When I run the program in emulator, sometimes it runs fine, but other times it shows "Application doing too much work in the main thread". So, am I doing something wrong? or is there a better way to do it?
Since nearly all of your posted code is running in a background thread, this would not cause your application to be doing too much work in the main thread. Your problem is likely coming from another source.
You could try profiling your app using traceview as described in the Android documentation here. Try to find which methods are consuming the most time to narrow down your search.
As an aside, you should use an IntentService instead of a Service if your service is just creating a single thread running this task in the background. You then implement the onHandleIntent method instead of the onStartCommand method. The IntentService will handle all operations it receives in a single background Looper.

Android Automatic Task in a specific time

I am new in Android development. I am developing a twitter client. I would like to send a tweet at every morning at 8. I want to set the schedule like Alarm. How can I do that ? I will be happy if you point me to some examples or other resources. Thanks.
Here is an example of the AlarmManager:
private void daylyTask()
{
daylyBR = new BroadcastReceiver() {
#Override public void onReceive( Context context, Intent _ )
{
//Do something
Log.d(TAG, "daylyTask uitgevoerd.");
}
};
getApplicationContext().registerReceiver( daylyBR, new IntentFilter("yourApp.blah") );
daylyPendingIntent = PendingIntent.getBroadcast( getApplicationContext(), 0, new Intent("yourApp.blah"), 0 );
GregorianCalendar cal = new GregorianCalendar();
cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
cal.set(GregorianCalendar.MINUTE, 0);
// set alarm to fire 5 sec (1000*5) from cal repeating every 86400000L ms (1 day)
manager.setRepeating( AlarmManager. RTC_WAKEUP, cal.getTimeInMillis() + 5000L, 86400000L, daylyPendingIntent );
}
Check out AlarmManager.
Alarms (based on the AlarmManager class) give you a way to perform time-based operations outside the lifetime of your application. For example, you could use an alarm to initiate a long-running operation, such as starting a service once a day to download a weather forecast.
Look up the AlarmManager API. I have used it to do things like what you are describing.
Note: The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running. For normal timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler.
http://developer.android.com/reference/android/app/AlarmManager.html

Android: Setting up a periodical alarm with AlarmManager

In my app, I need to add a row to a database and simultaniously set up an Alarm event to repeat every day at the time specified in one of the database columns. I already have some code, but it doesent trigger the alarm event at the specified time. Here is my code:
public class Add_reminder extends Activity {
AlarmManager am;
int hours, minutes;
REMIND_DB db;
Calendar calendar;
Cursor cursor;
Button button;
public void onCreate(Bundle savedInstanceState) {
//The usual code in the beginning of onCreate
//I load db from extended Application class as global since i use it in more
//Activities. Ints hours and minutes is set by user interaction
calendar = Calendar.getInstance();
am = (AlarmManager) getSystemService(ALARM_SERVICE);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
db.open();
db.insertReminder( -- parameters for database --);
cursor = db.getAllReminders();
cursor.moveToLast();
calendar.set(Calendar.HOUR, hours);
calendar.set(Calendar.MINUTE, minutes);
Intent intent = new Intent(Add_reminder.this, ReminderAlarm.class);
intent.putExtra("id_of_db_row", cursor.getInt(0));
PendingIntent pi = PendingIntent.getActivity(Add_reminder.this,
cursor.getInt(0), intent, PendingIntent.FLAG_CANCEL_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
24*3600*1000, pi);
db.close()
}
});
}
}
Database is updated correctly, but the ReminderActivity never starts on specified time. I dont know what could be wrong. I saw some example codes using BroadcastReceiver instead of starting the Activity right on with the PendingIntent, but this should work too, right? Does anyone knows what could be wrong?
My second question is if Im going to need the same instance of AlarmManager when I want to add or remove some alarms from a different Activity, or do I just declare another AlarmManager in every Activity I need?
Thank you!
You should use a broadcast receiver for alarms, and then start a service that does the actual work. Broadcast receivers shouldn't block the UI thread with lengthy operations (such as writing to the DB). Additionally, 'once a day' alarms might be problematic: if the user reboots the phone: registered alarms will be lost. You need to:
save the time the alarm is supposed to run to, say, SharedPreferecnes
re-register your alarm when the phone boots (receive the BOOT_COMPLETED broadcast)
don't use setRepeating() but let each alarm register the next on
Using a shorter period (1 or 2 minutes) for testing also helps.
As for the AlarmManager instance, it's a system service, you don't need to care about what instance you are using. Just get it using getSystemService()

Android AlarmManager execution time problem

I have set a AlarmManager which will give alarm repeatedly after certain time. I used following code for that.
Intent intent = new Intent(ViewDoughnut.this, NotificationMessage.class);
PendingIntent sender = PendingIntent.getBroadcast(ViewDoughnut.this, 0, intent,PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP,nextAlarmTime, alarmInterval, sender);
Now to execute proper work at the alarm time I have created the following class extending BroadcastReceiver. Now I need the time in millisecond when the Alarm work should execute in that class. How to get it?
public class NotificationMessage extends BroadcastReceiver {
// Display an alert that we've received a message.
// #Override
public void onReceive(Context context, Intent intent) {
// here I need the time when the alarm should execute.
}
}
Here I like to add, system time is not working for me, because if the device is switch off at the alarm time, it execute that when the device is on after that time. But I need the time when it should execute.
You could create a class that derives from Application which holds all global variables. Then just set a long variable to hold the time before initialising the alarm

Categories

Resources