1

I have some sensitive data that I load on the device in my application. And it can be reused between sessions / multiple uses of the app and be cleared when not in use / open / active.

So the app is not active / running. There is a notification showing that the sensitive information is still in memory.

But I want to give the user an option of clearing this data after a set amount of time.

So my question is how do I run some code after a set number of minutes?

Daniel Persson
  • 602
  • 7
  • 17
  • 1
    You can do it in lot of ways , check this https://stackoverflow.com/questions/3072173/how-to-call-a-method-after-a-delay-in-android. – Sunil Sunny Apr 02 '18 at 06:38
  • Possible duplicate of [How to call a method after a delay in Android](https://stackoverflow.com/questions/3072173/how-to-call-a-method-after-a-delay-in-android) – Iulian Popescu Apr 02 '18 at 06:42
  • First figure out weather you have this requirement Only during foreground session of your app or in background too . And edit your question accordingly. – ADM Apr 02 '18 at 06:45
  • @ADM thank you for telling me that I was unclear. The app may not be active / running – Daniel Persson Apr 02 '18 at 06:51
  • You should have mention it at first place . `Handler` is not a solution for such cases . Use `JobShedular`. Search for it and implement . It will make sure the Job is done. – ADM Apr 02 '18 at 06:54
  • @ADM I thought "between sessions" was clear enough :) – Daniel Persson Apr 02 '18 at 06:56
  • No that's not . You see that's why you get so many answer as `Handler`. The word background should be mentioned it changes the whole problem. – ADM Apr 02 '18 at 06:59
  • @lulian_Popescu, your right that it might look like this is a duplicate but the solutions suggested in the answer your talking about is for a running application but I have the problem that I want to clear the data between sessions. When the application is closed or not active. – Daniel Persson Apr 02 '18 at 08:23

2 Answers2

1

You can schedule an alarm using the AlarmManager and run your code from the BroadcastReceiver/Service. The alarm will trigger weather your app is dead or alive. (If it's dead it will be awaken)

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);

You can use this method to register a one time alarm. So if you want the alarm to trigger in 5 mins, the delayMillis value should be 5 * 1000 * 60

void registerOneTimeAlarm(PendingIntent pendingIntent, long delayMillis) {
    int SDK_INT = Build.VERSION.SDK_INT;
    long timeInMillis = System.currentTimeMillis() + delayMillis;

    if (SDK_INT < Build.VERSION_CODES.KITKAT) {
        alarmManager.set(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);
    } else if (SDK_INT < Build.VERSION_CODES.M) {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);
    } else {
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMillis, pendingIntent);
    }
}

Let's say you will use a receiver, it will look like this:

public class AlarmReceiver
    extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
      // Your code here
    }
}

And don't forget to add it in the Manifest:

<receiver android:name="your.package.AlarmReceiver"/>

IMPORTANT Registered alarms are cleared when the device reboots, you need to re-register the alarm after the device boots. (If this is what you really need)

HedeH
  • 2,869
  • 16
  • 25
  • Great answer. But I believe that after SDK 21 you should use the JobScheduler. I'm trying an implementation from this medium post out at the moment. Maybe your answer could include this as an option after SDK_INT >= 21 ? https://medium.com/google-developers/scheduling-jobs-like-a-pro-with-jobscheduler-286ef8510129 – Daniel Persson Apr 02 '18 at 09:26
  • @DanielPersson, I agree, the JobScheduler is the way to go on 21+, but that's much more code to write, and the code above is good enough to run short tasks. (Even on Oreo devices). For long tasks I would recommend the JobScheduler. From what I understand you need a relatively short task. – HedeH Apr 02 '18 at 09:34
  • Very true. My function is one line and should take a few milliseconds on any device so JobScheduler is overkill. Thanks for the answer. – Daniel Persson Apr 02 '18 at 09:44
  • And you gain a cross-version support. You're welcome :) – HedeH Apr 02 '18 at 09:45
-1

You can try this

new Handler().postDelayed(new Runnable() {
@Override
public void run() {

    //Actions to be performed 

}
 }, TIME_OF_DELAY);
R.Anjali
  • 181
  • 1
  • 9