运行应用程序两次工作 [英] Run App Twice To Work

查看:99
本文介绍了运行应用程序两次工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个Android应用程序,测试是否启用了手机上的某些安全功能。例如,如果你有密码登录启用或如果您的数据是加密您的手机上。

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 run 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.

我测试,如果这些功能在我的 MainActivity 文件中的 ONSTART()功能被启用。我包括以下功能code:

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

如果更多的code采用邮寄来回答这个问题,只是让我知道了,谢谢您的帮助。也许这个问题有事情做与 super.onStart();

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?

推荐答案

这里好解释如何应用程序生命周期的流程。 ONSTART()可以多次执行。你可以保持计数器你有多少次进入了这个方法,并采取不同的每个时间:

Here is good explanation how app lifecycle flows. onStart() can be executed many times. You can keep counter how many times you had entered this method and act differently on each time:

 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
      }
 }

要更清楚的生命周期,为什么你的OnStart()方法被调用两次,我建议有计数器和Log.i()在周期的各个重要的国家 - 至少在的onCreate()和onRestart()。

To be more clear about life cycle and why your onStart() method is called twice I suggest to have counter and Log.i() in each important state of the cycle - at least in onCreate() and onRestart().

请记住,应用程序保持在内存中,当你点击主页按钮。当您再次单击它重新启动已经运行的应用程序图标(调用onRestart(),然后ONSTART()方法和无的onCreate())。当你真的杀了你的应用程序真正然后顺序是的onCreate和ONSTART没有onRestart。有logcat的记录确实能帮助你了解应用程序生命周期流程,以及为什么你ONSTART()被调用两次或更多次。

Keep in mind that app stays in memory when you click Home button. When you click app icon again it restarts already running app (calls onRestart() and then onStart() methods and no onCreate() ). When you really kill you app for real then sequence would be onCreate and onStart without onRestart. Having logcat records really helps you to understand app lifecycle flow and why your onStart() is called twice or more times.

这篇关于运行应用程序两次工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆