Android 允许用户更改默认屏幕
我的应用程序允许用户在每次启动应用程序时选择默认屏幕,我使用SharedReferences来完成此操作。该应用程序在安装后第一次启动时会提示用户选择屏幕,该部分正常工作。然而,在允许用户更改默认屏幕的应用程序中,我使用相同的代码,它从不存储更改。为了使其正确保存,我需要更改什么Android 允许用户更改默认屏幕,android,Android,我的应用程序允许用户在每次启动应用程序时选择默认屏幕,我使用SharedReferences来完成此操作。该应用程序在安装后第一次启动时会提示用户选择屏幕,该部分正常工作。然而,在允许用户更改默认屏幕的应用程序中,我使用相同的代码,它从不存储更改。为了使其正确保存,我需要更改什么 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Choose a D
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose a Default Screen");
builder.setItems(R.array.openChoices, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
SharedPreferences settings = getPreferences(0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("start", item);
editor.commit();
//Mech = 0, E&M = 1
int choice = getPreferences(0).getInt("start", 3);
if(choice == 0){
Toast.makeText(setscreen.this, "Mechanics is now the default screen", Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(setscreen.this, physics.class);
startActivity(myIntent);
}
else if(choice == 1){
Toast.makeText(setscreen.this, "E&M is now the default screen", Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(setscreen.this, physicsem.class);
startActivity(myIntent);
}
}
});
尝试使用类似的方法获取对共享首选项对象的引用:
myPrefs = PreferenceManager.getDefaultSharedPreferences(ScreenSaverActivity.this);
我不确定您这样做的方式是否有效,但我从未见过这样做只是传递一个0。并且您确定您正在调用
编辑器.commit()代码>每次?你试过调用editor.apply()吗?editor.commit()不应该做同样的事情吗?是的,文档说他们也做同样的事情,我只是说值得一试:)。是的,我会试一试。也许commit()会使该值永久化,以便在以后更改时不会被覆盖?