有没有办法Context在静态方法中获取当前实例?
我正在寻找那种方式,因为我讨厌每次更改时保存"Context"实例.
我是Android开发新手,我想在安装后首先根据应用程序设置一些应用程序的属性.有没有办法找到应用程序第一次运行,然后设置其第一个运行属性?
我正在尝试创建一个弹出对话框,该对话框仅在应用程序首次运行后显示,它将提醒用户应用程序中的新更改.所以我有一个像这样的对话框弹出窗口:
new AlertDialog.Builder(this).setTitle("First Run").setMessage("This only pops up once").setNeutralButton("OK", null).show();
Run Code Online (Sandbox Code Playgroud)
一旦他们解雇它,它将不会回来直到下一次更新或他们重新安装应用程序.
如何将上面的对话框代码设置为仅运行一次?
我正在制作一个Android应用程序,用于测试手机上的某些安全功能是否已启用.例如,如果您启用了密码登录,或者您的手机上的数据已加密.
出于某种原因,应用程序必须运行两次才能测试并查看是否在手机上启用了这些安全功能,这是我正在尝试解决的问题.我希望它能够测试并查看在创建应用程序时以及第一次运行应用程序时是否启用了安全功能,而不是第二次运行应用程序.
我测试onStart()我的MainActivity文件中的函数是否启用了这些功能.我在下面列出了函数代码:
@Override
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@SuppressLint("NewApi")
public void onStart()
{
super.onStart();
//determine if phone uses lock pattern
//It returns 1 if pattern lock enabled and 0 if pin/password password enabled
ContentResolver cr = getBaseContext().getContentResolver();
lockPatternEnable = Settings.Secure.getInt(cr, Settings.Secure.LOCK_PATTERN_ENABLED, 0);//Settings.System
//returns 1 if pin/password protected. 0 if not
KeyguardManager keyguardManager = (KeyguardManager) getBaseContext().getSystemService(Context.KEYGUARD_SERVICE);
if( keyguardManager.isKeyguardSecure())
{
//it is pin or password protected
pinPasswordEnable=1;
}
else
{
//it is not pin or password protected
pinPasswordEnable=0;
}//http://stackoverflow.com/questions/6588969/device-password-in-android-is-existing-or-not/18716253#18716253
//determine if …Run Code Online (Sandbox Code Playgroud) 在每次应用程序启动时,我都会使用 ExistingPeriodicWorkPolicy.KEEP 将定期工作排入队列,以便在已有计划的情况下不会对任何其他工作进行排队。
假设在将来的应用程序更新中,我更改了工作的约束或周期,但保留原始的uniqueName. PeriodicWorkRequest由于使用相同,此更改会被忽略吗uniqueWorkName?
或者这不会成为问题,因为应用程序更新时,应用程序的所有工作都会被取消?
这里最好的方法是什么?
如何知道这是否是第一次启动应用程序?
如果您要回答,请添加完整的代码,因为我已经阅读了一些答案而且我不理解它们.
谢谢.