Reputation:
I have a Database which I need to delete at a certain day, how can I perform this task? I found this :
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
synchronized public void run() {
\\ here your todo;
}
}}, TimeUnit.MINUTES.toMillis(1), TimeUnit.MINUTES.toMillis(1));
But I'm not sure if it will "save the task" until the expiry day. Thanks
Upvotes: 5
Views: 5904
Reputation: 1939
The AlarmManager class enables the scheduling of repeated alarms that will run at set points in the future. The AlarmManager is given a PendingIntent to fire whenever an alarm is scheduled. When an alarm is triggered, the registered Intent is broadcast by the Android system, starting the target application if it’s not already running.
Create a class that inherits from BroadcastReceiver. In the onReceive method, which is called when the BroadcastReceiver is receiving an Intent broadcast, we will set up the code that runs our task.
AlarmReceiver.java
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// For our recurring task, we'll just display a message
Toast.makeText(arg0, "I'm running", Toast.LENGTH_SHORT).show();
}
}
We then need to register the BroadcastReceiver in the manifest file. Declare the AlarmReceiver in the manifest file.
<application>
.
.
<receiver android:name=".AlarmReceiver"></receiver>
.
.
</application>
In your calling Activity include the following instance variables.
private PendingIntent pendingIntent;
private AlarmManager manager;
In onCreate() we create an Intent that references our broadcast receiver class and uses it in our PendingIntent.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Retrieve a PendingIntent that will perform a broadcast
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
}
We then include the method that will set up the recurring alarms. Once set, the alarm will fire after every X time, here we are taking 10 seconds example you can simply calculate this in order to trigger it for every day.
public void startAlarm(View view) {
manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
int interval = 10000;
manager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
}
Next, we’ll also set up the cancelAlarm() method to stop the alarms, if needed.
public void cancelAlarm(View view) {
if (manager != null) {
manager.cancel(pendingIntent);
Toast.makeText(this, "Alarm Canceled", Toast.LENGTH_SHORT).show();
}
}
Upvotes: 0
Reputation: 34170
To do these you need to use Alaram Manager which will invke after given specific time.
First you need to declare broadcast receiver who will receive these alaram
public class DBActionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// perform delete operation here
}
}
Second, Now register alaram manager
AlarmManager alarms = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
DBActionReceiver receiver = new DBActionReceiver ();
IntentFilter filter = new IntentFilter("ALARM_ACTION");
registerReceiver(receiver, filter);
Intent intent = new Intent("ALARM_ACTION");
intent.putExtra("param", "My scheduled action");
PendingIntent operation = PendingIntent.getBroadcast(this, 0, intent, 0);
// invoke broadcast after one minute of my app launch
alarms.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(1000 * 60), operation) ;
Upvotes: 2