Java android应用程序首次启动时如何执行操作

Java android应用程序首次启动时如何执行操作,java,android,android-activity,Java,Android,Android Activity,当我的应用程序安装并第一次打开时(当有互联网连接时),我希望手机从我的服务器下载一些信息并将其插入数据库 如果信息未完全下载或根本未下载,则此活动应持续到完全下载为止 我该怎么做呢 对此使用SharedReference。创建一个布尔变量,并在操作完成后对其进行更改。在创建主/启动器活动时,检查此变量并相应地执行操作。一些伪代码 if (!sharedpreferences.getBoolean("isOpComplete", false)) { // perform my operat

当我的应用程序安装并第一次打开时(当有互联网连接时),我希望手机从我的服务器下载一些信息并将其插入数据库

如果信息未完全下载或根本未下载,则此活动应持续到完全下载为止


我该怎么做呢

对此使用SharedReference。创建一个布尔变量,并在操作完成后对其进行更改。在创建主/启动器活动时,检查此变量并相应地执行操作。一些伪代码

if (!sharedpreferences.getBoolean("isOpComplete", false)) {
    // perform my operation 
    performOperation();
}

performOperation() {
    // Operation complete
    SharedPreferences.Editor editor = sharedpreferences.edit();
    editor.putBoolean("isOpComplete", true);
    editor.commit();
}

对此使用SharedReference。创建一个布尔变量,并在操作完成后对其进行更改。在创建主/启动器活动时,检查此变量并相应地执行操作。一些伪代码

if (!sharedpreferences.getBoolean("isOpComplete", false)) {
    // perform my operation 
    performOperation();
}

performOperation() {
    // Operation complete
    SharedPreferences.Editor editor = sharedpreferences.edit();
    editor.putBoolean("isOpComplete", true);
    editor.commit();
}
使用首选项

在您的活动中:

private boolean isFirstLaunch() {
    // Restore preferences
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    boolean isFirstLaunch = settings.getBoolean("isFirstLaunch", true);
    Log.i(TAG + ".isFirstLaunch", "sharedPreferences ");
    return isFirstLaunch;
}
onCreate
调用

if (isFirstLaunch()) {
    Intent firstLaunchIntent = new Intent(this,
        GetStartedActivity.class);
    firstLaunchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(firstLaunchIntent);
    // set the bool to false in next activity !
    finish();
}
使用首选项

在您的活动中:

private boolean isFirstLaunch() {
    // Restore preferences
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    boolean isFirstLaunch = settings.getBoolean("isFirstLaunch", true);
    Log.i(TAG + ".isFirstLaunch", "sharedPreferences ");
    return isFirstLaunch;
}
onCreate
调用

if (isFirstLaunch()) {
    Intent firstLaunchIntent = new Intent(this,
        GetStartedActivity.class);
    firstLaunchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(firstLaunchIntent);
    // set the bool to false in next activity !
    finish();
}

在数据库、共享首选项或文件中添加一个标志,表明您已下载数据。在onResume中,检查该标志以及设备是否具有连接。如果该标志为false且您已连接,请尝试下载数据


如果成功,请更新标志。

在数据库、共享首选项或文件中添加一个标志,指示您已下载数据。在onResume中,检查该标志以及设备是否具有连接。如果该标志为false且您已连接,请尝试下载数据


如果成功,请更新标志。

我在哪里执行此操作,它是否在我的主活动类中?我在哪里执行此操作,它是否在我的主活动类中?我想,在第二个方法的末尾需要一个
commit()
。我想,在第二个方法的末尾需要一个
commit()