0

Two questions: 1) I want to add to my application the option to automatically logout after a few minutes of inactivity. What I mean by automatic logout is that the application will return to the first activity (initial state) after few minutes of inactivity. My first activity is a login form and after that the user have different choices on another activity.

I don't have any clue how I can do this automatic Logout. I am looking for suggestions...

2) If it is possible to be able to made an auto-destroy application. Let said someone stolen my phone and I send a text message or something and the phone identifies that and auto uninstall the application. Can that be possible?

dvv
  • 83
  • 2
  • 5

3 Answers3

0

Timer to schedule some task in the future

I dont know if you can delete yourself but here is a link how to delete apps. Deleting applications

Consider deleting only the data in the app instead of the whole app.

you might want to register a IntentReceiver and look for a special text message

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
n3utrino
  • 2,361
  • 3
  • 22
  • 32
0

1) You can setup a timer and when the user interacts with the app you cancel it, after the interaction you start it again. Then when timer expires you launch the login activity with and Intent using FLAG_ACTIVITY_CLEAR_TOP flag, this flag will finish all activities in the task that are over the root login activity.

2) You can setup an intent filter for SMS and when the desired SMS arrives you can disable the application so it can't be launched, check this question for doing it.

Community
  • 1
  • 1
Lucas S.
  • 13,391
  • 8
  • 46
  • 46
  • how do you know the user is interacting with the application in any of the activities? I don't think I should call to cancel the timer in every button listener... – dvv Mar 11 '11 at 07:17
  • You just need to override the Activity dispatchTouchEvent (http://developer.android.com/reference/android/app/Activity.html#dispatchTouchEvent%28android.view.MotionEvent%29) and implement the logic there. – Lucas S. Mar 11 '11 at 13:21
  • So, this mean that you override the dispatchTouchEvent in the inital class that extends activity and write a code like the one given by @rgmills? – dvv Mar 15 '11 at 15:38
  • I am not getting something right because I am having the following error: E/AndroidRuntime: java.lang.IllegalStateException: TimerTask is scheduled already. It seems that the code always enter in the TimeTask before anything. Any idea why? – dvv Mar 15 '11 at 17:28
0

Here is a snippet to use the Timer:

    Timer timeoutTimer;
    int INTERVAL = 300000; //5 minute interval

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        timeoutTimer = new Timer();
        TimerTask timeoutTask = new TimerTask() {
            @Override
            public void run() {
                finish();
            }
        }
    //anything else you want to do onCreate
    }

void setTimer() {
    t.schedule(timeoutTask, Calendar.getInstance().getTime(), INTERVAL);
}

void resetTimer() {
    timeoutTask.cancel();
    setTimer();
}

void someMethodTriggeredByUserAction() {
    //do some work?
    resetTimer();
}
rgmills
  • 383
  • 1
  • 8
  • I am confuse where this code could be implemented? I mean I have tha main activity (login activity) and a lot of other activity. If the user is in the x activity and being there and doing nothing I want the auto-logout. I understand the idea of your snippet but not where I code it? – dvv Mar 15 '11 at 15:56
  • There couple of things wrong or missing. If you create timeoutTask in th oncreate method you can not access it neither in setTimer() nor resetTimer(). Also you never call setTimer to be able to set the time for the code. I follow your idea but when I am implemetning it I get couple or runtimes error. – dvv Mar 15 '11 at 18:31