简体   繁体   English

未创建共享的首选项

[英]Shared preferences isn't created

I am using the shared preferences and on bootup of the application I am checking for some values: 我正在使用共享首选项,并且在应用程序启动时正在检查一些值:

public static String getFirsUsageAppFromSharedPreferences(Context context) {
    SharedPreferences prefs = context.getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
    return prefs.getString("firstUsage", null);
}

This is the first method that I am calling it. 这是我调用的第一个方法。 When I am installing from Android studio the application on a real phone, the shared preferences file is created in /data/data/my.package.name. 当我从Android Studio在真实电话上安装应用程序时,共享的首选项文件在/data/data/my.package.name中创建。 But when I am building an apk and installing that apk with: 但是当我构建一个apk并使用以下命令安装该apk时:

adb install com.my.package.name 亚行安装com.my.package.name

the shared preferences file isn't created and my application crushes. 共享的首选项文件未创建,我的应用程序崩溃了。 Anyone knows why the shared preferences aren't created when i am installing it via adb? 有人知道我通过adb安装共享首选项时为什么未创建共享首选项吗? I am building the application in android studio: Build -> Build APK(s). 我正在android studio中构建应用程序:构建->构建APK。

For anyone else who may have this issue, there is a difference if you install the application via android studio and if you use adb install. 对于可能遇到此问题的其他人,如果您通过android studio安装应用程序,并且使用adb install,则有所不同。 Via android studio is the same as if you have installed the application and clicked on the icon, so the MainActivity is started. 通过android studio就像您已经安装了应用程序并单击图标一样,因此启动了MainActivity。 In my case I was installing the application but wasn't clicking on the icon, but another application was calling a service. 就我而言,我正在安装该应用程序,但没有单击该图标,但是另一个应用程序正在调用服务。 In that case the code in MainActivity won't be executed until you open your application. 在这种情况下,除非打开应用程序,否则不会执行MainActivity中的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM