gpt4 book ai didi

android - 删除并安装应用程序后取回共享首选项

转载 作者:行者123 更新时间:2023-11-29 16:00:18 25 4
gpt4 key购买 nike

嗨, friend ,我正在尝试制作游戏,我需要创建游戏的演示版本

我需要一个按钮,当用户点击它时游戏运行但只有 3 次。

用户点击按钮 3 次后,应用会显示消息“你应该购买游戏”

我使用了 sharedpreferences 。 3 单击按钮后游戏显示消息,但是当用户删除应用程序并再次安装时,游戏不显示消息并运行游戏。

删除应用程序后是否有任何方法可以保存共享首选项?这是我的代码:

     Button frist = ((Button)findViewById(R.id.test));               final SharedPreferences check = getSharedPreferences("check",MODE_PRIVATE);               final Editor edit = check.edit();

frist.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {

if(check.getInt("check1",0)<3){
Intent intent = new Intent(MainActivity.this,choose_name.class
);
startActivity(intent);
edit.putInt("check1", check.getInt("check1",0)+1).commit();


}


}
});

编辑: friend 我正在尝试写入一个文件并将我的设置保存到其中,我可以将我的文件保存在用户看不到的任何目录中并且我的文件在用户删除应用程序后不会删除并且我不需要使用权限吗?

最佳答案

尝试 Backing up and restoring a user’s SharedPreferences 。这会将您的偏好备份到任何外部存储

在用户卸载应用程序之前,您可以对其进行备份。添加一些逻辑,如果文件存在于 External Storage 中,则将其恢复。

编辑

使用以下代码支持您的偏好。

Preferences stored in the SharedPreferences class get saved into /data/data//shared_prefs/_preferences.xml - backing up is easy, you just need to copy the contents of this file to external storage:

public static void backupUserPrefs(Context context) {
final File prefsFile = new File(context.getFilesDir(), "../shared_prefs/" + context.getPackageName() + "_preferences.xml");
final File backupFile = new File(context.getExternalFilesDir(null),
"preferenceBackup.xml");
String error = "";

try {
FileChannel src = new FileInputStream(prefsFile).getChannel();
FileChannel dst = new FileOutputStream(backupFile).getChannel();

dst.transferFrom(src, 0, src.size());

src.close();
dst.close();

Toast toast = Toast.makeText(context, "Backed up user prefs to " + backupFile.getAbsolutePath(), Toast.LENGTH_SHORT);
toast.show();
return;
} catch (FileNotFoundException e) {
error = e.getMessage();
e.printStackTrace();
} catch (IOException e) {
error = e.getMessage();
e.printStackTrace();
}

使用以下代码恢复偏好

But restoring provides more of a challenge, since the shared_prefs file isn’t directly writable by the app, and the SharedPrefs class doesn’t directly expose its functionality to serialise from xml. Instead you have to parse the XML yourself and push the values back in. Thankfully the XML file has a straightforward structure, so it’s easy to loop over the elements and turn these back into preferences.

public static boolean restoreUserPrefs(Context context) {
final File backupFile = new File(context.getExternalFilesDir(null),
"preferenceBackup.xml");
String error = "";

try {

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

Editor editor = sharedPreferences.edit();

InputStream inputStream = new FileInputStream(backupFile);

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

Document doc = docBuilder.parse(inputStream);
Element root = doc.getDocumentElement();

Node child = root.getFirstChild();
while (child != null) {
if (child.getNodeType() == Node.ELEMENT_NODE) {

Element element = (Element) child;

String type = element.getNodeName();
String name = element.getAttribute("name");

// In my app, all prefs seem to get serialized as either "string" or
// "boolean" - this will need expanding if yours uses any other types!
if (type.equals("string")) {
String value = element.getTextContent();
editor.putString(name, value);
} else if (type.equals("boolean")) {
String value = element.getAttribute("value");
editor.putBoolean(name, value.equals("true"));
}
}

child = child.getNextSibling();

}

editor.commit();

Toast toast = Toast.makeText(context, "Restored user prefs from " + backupFile.getAbsolutePath(), Toast.LENGTH_SHORT);
toast.show();

return true;

} catch (FileNotFoundException e) {
error = e.getMessage();
e.printStackTrace();
} catch (ParserConfigurationException e) {
error = e.getMessage();
e.printStackTrace();
} catch (SAXException e) {
error = e.getMessage();
e.printStackTrace();
} catch (IOException e) {
error = e.getMessage();
e.printStackTrace();
}

Toast toast = Toast.makeText(context, "Failed to restore user prefs from " + backupFile.getAbsolutePath() + " - " + error, Toast.LENGTH_SHORT);
toast.show();

return false;
}


Toast toast = Toast.makeText(context, "Failed to Back up user prefs to " + backupFile.getAbsolutePath() + " - " + error, Toast.LENGTH_SHORT);
toast.show();

}

Finally, you need to restart your app before these preferences will take hold:

if (restoreUserPrefs(context)) {
// Restart
AlarmManager alm = (AlarmManager) context.getSystemService(
Context.ALARM_SERVICE);
alm.set(AlarmManager.RTC,
System.currentTimeMillis() + 1000, PendingIntent.getActivity(context, 0,
new Intent(context, MainActivityName.class), 0));
android.os.Process.sendSignal(android.os.Process.myPid(),
android.os.Process.SIGNAL_KILL);
}

关于android - 删除并安装应用程序后取回共享首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25054480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com