运行应用程序两次工作

pHo*_*pec 20 java android

我正在制作一个Android应用程序,用于测试手机上的某些安全功能是否已启用.例如,如果您启用了密码登录,或者您的手机上的数据已加密.

出于某种原因,应用程序必须运行两次才能测试并查看是否在手机上启用了这些安全功能,这是我正在尝试解决的问题.我希望它能够测试并查看在创建应用程序时以及第一次运行应用程序时是否启用了安全功能,而不是第二次运行应用程序.

我测试onStart()我的MainActivity文件中的函数是否启用了这些功能.我在下面列出了函数代码:

    @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
Run Code Online (Sandbox Code Playgroud)

如果需要发布更多代码来回答这个问题,请告诉我,谢谢你的帮助.也许问题与这个问题有关super.onStart();

有人认为启动加载屏幕可能有助于解决问题吗?

小智 8

这里有很好的解释应用程序生命周期的流程.onStart()可以执行多次.您可以保留计数器输入此方法的次数,并且每次都采取不同的行动:

 static int counter=0;
 public void onStart()
    {
      counter++;
      Log.i("MyApp", "onStart() run "+counter);
      switch (counter){
        case 1: break; // first run
        case 2: break; // second run
        default: break;// other runs
      }
 }
Run Code Online (Sandbox Code Playgroud)

为了更清楚生命周期以及为什么你的onStart()方法被调用两次,我建议在循环的每个重要状态中都有计数器和Log.i() - 至少在onCreate()和onRestart()中.

请记住,当您单击"主页"按钮时,应用程序会保留在内存中.再次单击应用程序图标时,它会重新启动已运行的应用程序(调用onRestart()然后调用onStart()方法,而不调用onCreate()).当你真正杀死你的应用程序真实然后序列将onCreate和onStart没有onRestart.拥有logcat记录确实可以帮助您了解应用程序生命周期流以及为什么onStart()被调用两次或更多次.