【问题标题】:Run Android App Twice To Work, Why?运行两次 Android 应用程序,为什么?
【发布时间】:2015-05-01 03:30:54
【问题描述】:

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

由于某种原因,该应用程序必须运行两次以测试手机是否启用了这些安全功能,这就是我要解决的问题。我希望它测试并查看在创建应用程序和第一次运行应用程序时是否启用了安全功能,而不是第二次运行它。

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

    @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

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

有关

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

【问题讨论】:

  • 我的第一个想法是询问getBaseContext 的设置位置。如果你使用ApplicationContext,你有同样的问题吗?您的 onResume 生命周期函数中有什么?
  • 我目前没有onResume 函数。只需onStartonCreate。我用ApplicationContext 更改了所有getBaseContext 的出现,它仍然需要运行2 次才能工作。
  • 第一次运行会发生什么?
  • Run App Twice To Work 的可能重复项

标签: java android super oncreate onstart


【解决方案1】:

super.onStart();很好。启动画面无济于事。

从您的代码中,我看不出您如何确定它运行了多少次。 您还提到了测试-是手动测试还是使用任何框架?也许你的框架有一些在每次运行之前运行的 init 方法,它对 onStart() 进行了额外的调用。

问题不在此代码中。使用调试器或 logcat 并找出谁给你打了两次电话,正如 @nasch 所问的,第一次运行时会发生什么。

不过,要帮助您的真正问题仍然存在 - 您是什么意思“呼叫两次”。是您手动单击应用程序图标两次还是某些测试框架调用您的应用程序两次。这两种情况都很容易解决。

【讨论】:

    猜你喜欢
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    相关资源
    最近更新 更多