全新安装后只运行一次代码,共享首选项不是解决方案,请参见下面的描述 [英] Run code only once after a fresh installation,Shared Preference is not a solution please see below description

查看:23
本文介绍了全新安装后只运行一次代码,共享首选项不是解决方案,请参见下面的描述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 android 应用程序中只运行一次特定的代码.共享首选项解决方案不是解决方案,因为当您转到应用程序管理器并执行清除数据然后共享首选项被删除,因此应用程序将其视为新的全新安装.我什至尝试过失败的 Application 类,它的工作方式与共享首选项相同.除了共享首选项和应用程序类之外的任何帮助将不胜感激.

I want to run a specific code only once in my android application.Shared preferences solution is not the solution as when you go to application manager and perform Clear Data then shared preferences gets deleted so application treats it a new fresh installation. I even tried Application class that too failed,it works same as shared preferences. Any help except Shared preferences and Application Class will be appreciated.

谢谢.

推荐答案

答案取决于只有一次"的含义.

The answer hinges on what "only once" means.

设置SharedPreference.

如果用户清除数据,或卸载然后重新安装,代码将再次运行.

If the user clears data, or uninstalls then reinstalls, the code will be run again.

外部存储.

你可以这样做:

  • 在应用程序自己的存储中,将在卸载时清除,但不会在用户点击清除数据时清除.
  • 共享外部存储中.
  • in the application's own storage, which will be cleared at uninstall but not when the user hits Clear Data.
  • in shared external storage.

第二种方法很有前景:它可以抵抗清除数据并重新安装.

This second approach is promising: it is resistant to Clear Data and reinstalls.

但是,在存储可移动或不可挂载的情况下使用外部存储是很棘手的,如果存储不可用,我不确定您的回退是什么.(提示:快速失败).

However, using external storage where the storage is removable, or unmountable is tricky, and I'm not sure what your fallback would be if the storage isn't available. (clue: fail fast).

您也可能不相信您的用户不会(意外或故意)删除小心放置的文件.

You also may not trust your users not to (accidentally or deliberately) delete the carefully placed files.

我认为这超出了这个问题的范围.但您应该首先查看 AccountManager 文档,然后从那里开始.

I think this is out of scope for this question. But you should start by looking at AccountManager docs, and go from there.

好的,所以:

  • 如果无法读取外部存储,我们不希望快速失败.
  • 我们不想用上面的方法来解决问题,因为坏人会删除外部存储上的标志文件,并且清除数据.
  • 我们必须为每个设备做一次永远,即使设备恢复出厂设置并且外部存储被擦除?
  • we don't want to fail fast if we can't read external storage.
  • we don't want to do belt and braces with the above, because bad actors delete the flag files on external storage, and Clear Data.
  • we must only do once per device ever, even if the device is factory reset and external storage is wiped?

我们必须使用可以存储此设备状态的外部源进行检查;我们称之为服务器".

We'll have to check that with an external source which can store the state for this device; let's call that a "server".

我们需要一个标识符来向服务器唯一标识自己.

We need an identifier to uniquely identify ourselves to the server.

通常,您会生成一个 UUID,并将其存储在某处.但我们不能相信我们的任何存储选项.

Ordinarily, you would generate a UUID, and store it somewhere. But we can't trust any of our storage options.

所以我们需要从我们的静态外部环境中生成一个标识符.iPhone 现在 已弃用的 UDID 正是这样,从各种硬件标识符生成.

So we need to generate an identifier from our static external environment. iPhone's now deprecated UDID was exactly this, generated from various hardware identifiers.

复制此链接将是一个很好的开始,但取决于您的安全许可,您可能想要你自己.如果世界上每个人都使用您的应用或使用与您的应用相同的算法,则可能会涉及隐私(这就是 Apple 弃用 UDID 的原因,以及每个应用应使用自己的 UUID 的原因).

Copying this link would be a great start, but depending on your security clearance you may want to make your own. There may be privacy implications if everyone in the world used your app or the same algorithm as your app (this is why Apple deprecated UDID, and why each app should use its own UUID).

无论哪种方式,对于(充其量)边缘情况,这都是极其大量的工程工作(包括服务器),因此我会避免它.

Either way, this is an extremely large amount of engineering effort (including the server) for (at best) an edge case, so I'd avoid it.

更糟糕的是,它会将您的应用与互联网连接联系起来,这取决于您的具体情况,这可能是件坏事.

Worse, it ties your app to having an internet connection, which depending on your context, may be a bad thing.

此外,有根设备将有权更改任何或所有这些标识符.之后就有点哲学了.

Furthermore, a rooted device will have access to change any or all of these identifiers. It gets a bit philosophical after that.

手动完成.说真的,在部署之前或之后的某个时间手动执行.

Do it manually. Seriously, do it by hand, either before you deploy, or sometime after.

如果您需要选择获胜的设备,那么您需要一个服务器和某种识别设备的方法,如上所述.手动完成,然后选择然后告诉获胜者.但这也超出了这个 Android 问题的范围.

If you need to pick a winning device, then you need a server and some way of identifying the device, as above. Do it manually, then pick then tell the winner. But that's also out of scope for this Android question.

这篇关于全新安装后只运行一次代码,共享首选项不是解决方案,请参见下面的描述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆