0

Code for checking if user is browsed out from application:

public boolean isApplicationSentToBackground(final Context context) {
    try {
        ActivityManager am = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
        if (!tasks.isEmpty()) {
            ComponentName topActivity = tasks.get(0).topActivity;
            if (!topActivity.getPackageName().equals(
                    context.getPackageName())) {
                return true;
            }
        }
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return false;
}

protected void onPause() {
    Log.i("Status", "onPause");
    super.onPause();

    SharedPreferences prefs =
        PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean security = prefs.getBoolean("prefSecurity", false);

    try{
        if(isApplicationSentToBackground(getApplicationContext()) == false){
            if(security == true)  {
                SharedPreferences settings = getSharedPreferences(PREFS_NAME,
                    MODE_PRIVATE);
                SharedPreferences.Editor editor = settings.edit();
                editor.putString("prefSecurityCheck", "putsis");
                editor.commit();
        }
      }
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Something Went Wrong!",
            Toast.LENGTH_SHORT).show();
    }
}

I have made that user can have an option to enter a security code before he can see the app. And it checks from sharedpreferences if we need to ask the security code, if it saves "putsis" then the next time we open mainactivity it asks for security code, and when right code is entered then it changes prefSecurityCheck to "korras" and no code check is made.

Right now two problems are that:

When I go to app settings or press share button it counts like I've went out of the app.

If i press recent button or sometimes home button and reopen it, it won't always show security screen, I think it is due to the fact that it doesn't save those preferences fast enough to sharedpreferences.

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
  • the onPause - is from main activity ? Also please do not compare booleans to true or false - it is very confusing - you should write `if(!isApplicationSentToBackground(getApplicationContext())){ if(security)` – Mr_and_Mrs_D Dec 22 '13 at 23:23
  • Yes, onPause is from main activity? And thanks, I will sure keep that in mind – Anonüümne Rüütel Dec 22 '13 at 23:37
  • your first problem I do not understand - for your second - are you sure the pref is written ? And I guess you check onResume() - post this code. Also - why are you passing all sort of contexes ? Why not just `this` ? – Mr_and_Mrs_D Dec 22 '13 at 23:50
  • My app has share button and a settings button on actionbar, when i go into settings or press the share button it counts like i have browsed away from app. And yes, the pref is written, just it somehow doesn't update always. And no there isn't onResume(), the fact if to check for security or not is checked onCreate – Anonüümne Rüütel Dec 22 '13 at 23:53
  • " when i go into settings or press the share button it counts like i have browsed away from app" - you have your mainactivity displayed and go to settings - have `isApplicationSentToBackground` print the context - it is probably the action bar application - get this context and have `isApplicationSentToBackground` return false for this. There is no way the pref is not updated - try writting in the default shared preferences instead of the `PREFS_NAME` - and get rid of base context – Mr_and_Mrs_D Dec 23 '13 at 00:31
  • Add also debug prints when you write the `prefSecurityCheck` - it must not be written - which is probably the fault of `isApplicationSentToBackground` – Mr_and_Mrs_D Dec 23 '13 at 00:44

0 Answers0