Situation
I have a timeout mechanism in my app which prompts the user after a certain duration of inactivity to enter a PIN-Code. To do so i start a new activity on top of the currently active one. The user then has a few tries to enter the correct PIN. If there are no tries remaining the app resets and shuts it self down.
To realize the timer i have a background thread which is notified for every touch event that occurs in my app. Every time the screen is touched the inactivity timer resets. If the Thread hasn't been notified until the duration is up it starts the PIN-Activity.
public class TimeoutThread extends Thread {
private final Semaphore semaphore = new Semaphore(0);
protected TimeoutThread() {
super(TimeoutThread.class.getSimpleName());
}
@Override
public void run() {
while (!isInterrupted()) {
try {
if (!semaphore.tryAcquire(appData.getDisplayTimeoutTime(), TimeUnit.MILLISECONDS)) {
// this is where i start the PIN-Activity
semaphore.acquire();
}
} catch (InterruptedException ignored) {
break;
}
}
contextWeakReference.clear();
}
protected void resetTimer() {
if (semaphore.availablePermits() == 0) {
semaphore.release();
}
}
}
This Thread is located in my Application
class. It starts in the Applications onCreate
and therefore runs from Appstart on.
Now as i said, if the wrong PIN is entered, the app should shut down. To do so i have a shutdown method in my Application which interrupts the Thread, sets its reference to null and calls System.exit(0)
.
Problem
No matter what i try, android restarts my app, seemingly because of my Thread. If i don't start it, start my PIN-Activity manually and enter the wrong PIN it shuts down just fine. The Thread is correctly stopped tho and dead by the time System.exit(0) is called. I do not understand how a dead Thread could cause a restart. The only other difference i can think of is that my PIN-Activitys Intent is startet from the UI-Thread if i manually navigate to it, while the app-timeout starts it from the timeout Thread.
Question
Why is this happening and how could i fix it? Does the Thread that calls the startActivity(Intent) method effect the result?
What i also tried
I have tried to shut down my app using System.exit()
, Activity#finishAndRemoveTask()
(on all remaining Activities) and android.os.Process.killProcess(myPid)