我想创建仅出现一次启动屏幕的应用。就像我刚从Playstore下载应用程序时,以及我按应用程序图标时一样。仅第一次显示启动屏幕。在剩下的时间里。它不会像进行第二项活动那样直接打开。我怎样才能做到这一点。谢谢。
您可以通过创建没有UI和SharedPreferences的启动器活动来实现:
public class LauncherActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preferences = getSharedPreferences("App", Context.MODE_PRIVATE);
// Get value from shared preferences,
// if null (app is run first time) the default value (second argument) is returned
boolean isFIrstRun = preferences.getBoolean("isFirstRun", true);
if (isFIrstRun) {
// set isFirstRun to false
preferences.edit().putBoolean("isFirstRun", false).apply();
// launch splash screen activity
} else {
// Launch other activity
}
}
}
并确保将LauncherActivity设置为应用manifest.xml中的启动器活动:
<activity android:name=".LauncherActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>