Run App Twice To Work

前端 未结 4 1110

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 you

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-05 08:05

    I'm not sure why you are using onStart(), if you want it to be run the first time the activity is created I would probably use onCreate() instead.

    There is no API in Android that will tell you if the app has been run at least once so you will need to use some type of persistent storage for that, e.g. SharedPreferences could be used to persist a flag that would be set the first time your app is run and thereafter you can check it as shown here.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        SharedPreferences settings = getSharedPreferences("Prefs", 0);
        if (settings.getBoolean("first_time", true)) {
            //the app is being launched for first time, do something        
            Log.d("Comments", "First time");
    
            // first time task
    
            // record the fact that the app has been started at least once
            settings.edit().putBoolean("first_time", false).commit(); 
        }
    }
    

提交回复
热议问题