检查应用程序在其首次运行首次、应用程序

2023-09-12 21:34:09 作者:我走过的孤独很黑

我是新android开发和,我想安装一些应用程序的属性,根据应用程序安装后第一次运行。有没有办法找到该应用程序正在运行的第一次,然后设置其第一次运行的属性?

I am new to android development and and I want to setup some of application's attributes based on Application first run after installation. Is there any way to find that the application is running for the first time and then to setup its first run attributes?

推荐答案

下面是使用共享preferences的例子来实现了第一次运行检查

The following is an example of using SharedPreferences to achieve a 'first run' check.

public class MyActivity extends Activity {

    SharedPreferences prefs = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Perhaps set content view here

        prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (prefs.getBoolean("firstrun", true)) {
            // Do first run stuff here then set 'firstrun' as false
            // using the following line to edit/commit prefs
            prefs.edit().putBoolean("firstrun", false).commit();
        }
    }
}