5

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

  • A answer in [this link](http://stackoverflow.com/a/8801990/3800164) will help you to schedule the task in future. – android_griezmann Nov 21 '16 at 09:25
  • My understanding is that if your app were to exit _before_ the task were spawned, then no, it would not execute. If your app attempted to exit _while_ the task were running, then the OS might block it. The answer below by @jitesh might be what you are looking for. – Tim Biegeleisen Nov 21 '16 at 09:31

2 Answers2

2

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) ; 
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
0

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();
    }
}
U.Swap
  • 1,921
  • 4
  • 23
  • 41
  • Hi, I'm using Realm Database, but i need to pass a Context which is a `Activity`, how can I pass this of `onReceive` ? thanks –  Nov 21 '16 at 15:39
  • It's not working..."Cannot resolve the constructor Intent " `Intent alarm = new Intent(this,AlarmReceiver.class);` –  Nov 22 '16 at 00:05