5

I'm making an android app that test if certain security features on your phone are enabled. For example, if you have password log in enabled or if your data is encrypted on your phone.

For some reason, the app has to be ran twice to test and see if these security features are enabled on the phone or not, and this is the problem I'm trying to solve. I'd like it to test and see if the security features are enabled when the app is created and the first time the app is run, not the second time it is run.

I test if these features are enabled in the onStart() function in my MainActivity file. I included the functions code below:

    @Override
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    @SuppressLint("NewApi")
    public void onStart()
    {
        super.onStart();

        //determine if phone uses lock pattern
        //It returns 1 if pattern lock enabled and 0 if pin/password password enabled
        ContentResolver cr = getBaseContext().getContentResolver();
        lockPatternEnable = Settings.Secure.getInt(cr, Settings.Secure.LOCK_PATTERN_ENABLED, 0);//Settings.System 


        //returns 1 if pin/password protected. 0 if not
        KeyguardManager keyguardManager = (KeyguardManager) getBaseContext().getSystemService(Context.KEYGUARD_SERVICE);
        if( keyguardManager.isKeyguardSecure()) 
        {
           //it is pin or password protected
           pinPasswordEnable=1;
        } 
        else 
        {
           //it is not pin or password protected 
            pinPasswordEnable=0;
        }//http://stackoverflow.com/questions/6588969/device-password-in-android-is-existing-or-not/18716253#18716253

        //determine if adb is enabled. works
        adb=Settings.Global.getInt(cr, Settings.Global.ADB_ENABLED, 0);

        //determine if bluetooth is enabled.works
        bluetooth=Settings.Global.getInt(cr, Settings.Global.BLUETOOTH_ON, 0);
        //Settings.System BLUETOOTH_DISCOVERABILITY

        //determine if wifi is enabled. works
        WifiManager wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
        if (wifi.isWifiEnabled())
        {
            //wifi is enabled
            wifiInt=1;
        }
        else
            wifiInt=0;

        //determine if data is encrypted
        getDeviceEncryptionencryption();

        //determine if gps enabled


    }//end of onStart() function

If any more code needs to be posted to answer this question, just let me know, and thanks for your help. Maybe the issue has something to do with the super.onStart();

Does anyone think that a splash loading screen might help solve the issue?

pHorseSpec
  • 1,246
  • 5
  • 19
  • 48
  • My first thought is to ask where `getBaseContext` is being set. If you use `ApplicationContext` do you have the same problem? What do you have in your `onResume` lifecycle function? – Grambot May 01 '15 at 15:16
  • I currently don't have an `onResume` function. Just `onStart` and `onCreate`. I changed all occurences of `getBaseContext` with `ApplicationContext` & it still requires 2 runs to work. – pHorseSpec May 01 '15 at 15:27
  • What happens the first time it's run? – nasch May 01 '15 at 22:32
  • possible duplicate of [Run App Twice To Work](http://stackoverflow.com/questions/24046132/run-app-twice-to-work) – Jonas Czech May 03 '15 at 13:59

1 Answers1

1

super.onStart(); is fine. Splash screen will not help.

From your code I do not see how you determine how many times it ran. You also mention testing - is it manual testing or you use any framework? Maybe your framework has some init method which runs before each run and it makes this extra call for onStart().

Issues is not in this code. Use debugger or logcat and figure out who calls you twice and, as @nasch had asked, what happens at first run.

Still, real question to help you remains - what do you mean "call twice". Is it you manually clicking app icon twice or is it some testing framework calls your app twice. Both cases are clear to solve.

Alex
  • 4,457
  • 2
  • 20
  • 59