在android中第一次启动应用程序时,只显示一次消息对话框

use*_*633 7 android

我在android中开发了一个应用程序,其中一个重要的要求是只在应用程序第一次运行时显示语言支持的消息对话框,然后每次用户再次运行应用程序时它都会消失,我我试图使用共享偏好但它没有用,有没有其他方法可以做到这一点?

ask*_*msk 16

在onCreate处理程序中使用此函数:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    if (isFirstTime()) {
        // show dialog
    }
    ...
}


/***
 * Checks that application runs first time and write flag at SharedPreferences 
 * @return true if 1st time
 */
private boolean isFirstTime()
{
    SharedPreferences preferences = getPreferences(MODE_PRIVATE);
    boolean ranBefore = preferences.getBoolean("RanBefore", false);
    if (!ranBefore) {
        // first time
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean("RanBefore", true);
        editor.commit();
    }
    return !ranBefore;
}
Run Code Online (Sandbox Code Playgroud)

注意:它需要访问存储上的文件的权限:android.permission.WRITE_EXTERNAL_STORAGE