简体   繁体   English

对 android 应用程序进行编程:如何使其显示一次性设置屏幕

[英]For programming an android app: How do I make it display a one time setup screen

While searching this question I have seen this.在搜索这个问题时,我看到了这个。 One time Android setup screen? 一次Android设置画面?

It asks my question, but I can never get it to work.它问我的问题,但我永远无法让它工作。 What I want to happen is when I first startup the app, it gives me the setup screen.我想要发生的是当我第一次启动应用程序时,它给了我设置屏幕。 Then when I press save, I want it to quit the app.然后当我按保存时,我希望它退出应用程序。 When I click the app again, I want it to preform a task, rather than show up with a screen.当我再次单击该应用程序时,我希望它执行一项任务,而不是显示一个屏幕。 So thats really 2 questions, how to make it show a setup screen one time, and then how to make it do an action(by clicking on the app) without a screen showing up at all.所以这真的是两个问题,如何让它一次显示一个设置屏幕,然后如何让它做一个动作(通过点击应用程序)而不显示一个屏幕。

Right now, I use SharedPreferences editor to input my settings.现在,我使用 SharedPreferences 编辑器来输入我的设置。

Pretty simple, in the onCreate of your activity:非常简单,在您的活动的 onCreate 中:

   savedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    if(savedPreferences.getBoolean(PREF_SHOW_ABOUT_ON_APP_START, true)){        
        Intent intent = new Intent(this, SetupActivity.class);
                    startActivity(intent);
                    savedPreferences.edit().putBoolean(PREF_SHOW_ABOUT_ON_APP_START, false).commit(); // YOu could do this line within the SetupActivity to ensure they have actually done what you wanted
                    finish();
    } else {
                 // Go somewere else
            }

You don't have to activity switch, but you get the picture您不必进行活动切换,但您会得到图片

For the first time only set up screen, you simply have to use SharedPreferences.第一次只设置屏幕,您只需使用 SharedPreferences。 Check if a value, alreadyLaunched exists in the shared preferences.检查共享首选项中是否存在已启动的值。 If not, show the set up screen.如果没有,请显示设置屏幕。 If yes show your app.如果是,请显示您的应用程序。

For the second part of your question, it would be considered very bad practice to have an icon app that does nothing at all when you click it.对于您问题的第二部分,当您单击它时,拥有一个根本不执行任何操作的图标应用程序将被认为是非常糟糕的做法。 How would the user know that it worked or did what it was supposed to do?用户如何知道它工作或做了它应该做的事情?

For the "I want a task to execute but I don't want the user to see it happening", I believe you should look at services.对于“我想要执行任务但我不希望用户看到它发生”,我相信你应该看看服务。 When your user closes the app clicking the save button on the set up screen, start a service ( http://developer.android.com/reference/android/app/Service.html )当您的用户单击设置屏幕上的保存按钮关闭应用程序时,启动服务( http://developer.android.com/reference/android/app/Service.html

When the user clicks again on your app, simply show a message and make the app call the service via an intent.当用户再次点击您的应用时,只需显示一条消息并让应用通过意图调用服务。

Probably you are forgetting to call可能你忘记打电话了

myEditor.commit()

after you are done with setting your preferences.完成设置偏好后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM