运行code,全新安装后只有一次,共享preference不是解决办法,请参见下面的说明 [英] Run code only once after a fresh installation,Shared Preference is not a solution please see below description

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

问题描述

我想运行一个特定的code只有一次在我的Andr​​oid application.Shared preferences解决方案是解决不了问题的,当你去应用程序管理器,并执行被删除,以便清除数据,然后共享preferences应用程序会把它一个新的全新安装。 我甚至试过,太失败了应用类,它的工作原理相同的共享preferences。 除了共享preferences和应用类任何帮助将AP preciated。

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.

设置共享preference

如果用户清除数据,或将卸载再重新安装,在code将再次运行。

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

保存空文件(一个标志文件)在外部存储。

Save a empty file (a flag file) in a well-known location on external storage.

您可以做到这一点:

  • 在应用程序本身的存储,这将在卸载被清除,但不是当用户点击清除数据
  • 在<一个href="https://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory%28%29">shared外部存储。
  • 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.

我觉得这是超出范围了这个问题。但是,你应该通过查看<一个开始href="https://developer.android.com/reference/android/accounts/AccountManager.html">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的现在<一href="http://www.insidemobileapps.com/2011/08/20/as-apple-sets-out-to-de$p$pcate-udids-developers-look-for-alternatives/">de$p$pcated 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.

复制此链接将是一个很好的开始,但根据您的安全许可,你可能想使自己的。有可能是隐私问题,如果每个人都在世界上使用您的应用程序或者相同的算法您的应用程序(这就是为什么苹果去precated 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.

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

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