代码之家  ›  专栏  ›  技术社区  ›  rated2016

我的Android应用程序存在奇怪的共享引用问题

  •  1
  • rated2016  · 技术社区  · 6 年前

    问题是设置为sharedPreferences的默认值正在更改,这取决于我是从Google Play Store安装应用程序,还是直接安装APK(或从Play Store安装后清除应用程序存储)。

    从Play Store安装时,我的布尔值为false。

    清除存储并再次运行,我的布尔值为真。

    private void setShowStoragePermission(boolean permission){
        SharedPreferences sharedPreferences  = getSharedPreferences("storagePermission", MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean("permission", permission).apply();
    }
    
    private boolean getShowStoragePermission(){
        SharedPreferences sharedPreferences  = getSharedPreferences("storagePermission", MODE_PRIVATE);
        return sharedPreferences.getBoolean("permission", true);
    }
    

    在第一次干净运行时,没有保存的sharedreferences,因此返回的值 getShowStoragePermission()返回true。但是,从Play Store安装时,如Toast在前几秒钟看到的那样,我会出错(“savedpermission:false”)。

    你可以自己在 测试版 应用程序的: Link to app on Play store / /

    我正在Android P DP4上测试这个应用程序。这个问题有什么解决办法吗?

    主活动中的代码:

    if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            setToast("savedPermission: " + getShowStoragePermission(), Toast.LENGTH_LONG);
            //on first run from Google Play store @getShowStoragePermission returns false - need to return true
            if(getShowStoragePermission()) {
                ActivityCompat.requestPermissions(MainActivity.this,
                        new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
            } else {
                makeBingWallRequest(false);
            }
        }else{
            //storage permission granted manually from app settings or from dialog request
            createImageDir();
            makeBingWallRequest(true);
        }
    

    如果设置了sharedreferences:

        private void makeBingWallRequest(boolean storagePermission){
        if(getDeviceInternetStatus(context) == null && getBingWallDay() == 0){
            bingImage.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.bearcats));
            return;
        }
        if(!storagePermission){
           new BingWallpaper(MainActivity.this).execute(false);
        }else {
            Calendar now = Calendar.getInstance(TimeZone.getDefault());
            int day = now.get(Calendar.DAY_OF_MONTH);
    
            //on first run
            setToast("savedBingWallDay: " + getBingWallDay(), Toast.LENGTH_LONG);
            if(getBingWallDay() == FIRST_RUN_BING_IMAGE){
                saveBingWallDay();
                new BingWallpaper(MainActivity.this).execute(true);
                //every other run
            }else if (day != getBingWallDay()) {
               //if current time is past 3AM then make new request
                int hourIn24 = now.get(Calendar.HOUR_OF_DAY);
                if(hourIn24 >= BING_IMAGE_RESET_HOUR_IN_24){
                    saveBingWallDay();
                    new BingWallpaper(MainActivity.this).execute(true);
                }else loadBitmap();
            } else loadBitmap();
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Arseny Levin    6 年前

    我有类似的症状,后来才发现 自动备份 功能(与Android 6一起推出)。卸载并不能清除这些数据,因为它是从谷歌的云(仅仅是你的谷歌驱动器)中提取的。

    阅读更多信息: https://developer.android.com/guide/topics/data/autobackup

    要测试这一点,只需禁用备份功能,然后重复测试以确保获得预期的默认值。集合 android:allowBackup false :

    <manifest ... >
        ...
        <application android:allowBackup="false" ... >
            ...
        </application>
    </manifest>
    

    另外,我也不能但不敦促您不要将权限状态存储在本地磁盘上。为什么不打电话 Context.checkCallingOrSelfPermission() 在运行时?这是更安全的方法。