Java – Run Android app twice to work, why?

Run Android app twice to work, why?… here is a solution to the problem.

Run Android app twice to work, why?

I’m making an Android app to test if some security features on your phone are enabled. For example, if you have password sign-in enabled, or your data is encrypted on your phone.

For some reason, the app has to run twice to test if the phone has these security features enabled, and that’s what I’m trying to solve. I want it to be able to test and see if security features are enabled when the application is created and the first time the application runs, not the second run.

I test if these features are enabled in the function in onStart() my MainActivity file. I included the function 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 you need to post more code to answer this question, please let me know and thank you for your help. Maybe this question is related to super.onStart();

relate

Does anyone think that the boot loading screen might help solve the problem?

Solution

super.onStart(); Very good. The splash screen doesn’t help.

I don’t see from your code how you can determine how many times it ran.
You also mentioned testing – is it manual testing or is it using any framework? Maybe your framework has some init method that runs before each run, and it additionally calls onStart().

The problem is not in this code. Use a debugger or logcat to find out who called you twice and, as @nasch asks, what happens on the first run.

Still, the question that can really help you remains – what does “call twice” mean. Whether you manually click the application icon twice or some test framework calls your application twice. Both cases can be solved.

Related Problems and Solutions