2015-12-11 35 views
1

我正在開發一個應用程序,其中包含com.example1,我正在做另一個模塊以用作包com.example2的包。SharedPreferences在不同包裹之間管理

在我的模塊(com.example2)我使用這個靜態函數:

public static void saveLastDownload(Activity mActivity, long numberLong) { 
     SharedPreferences sharedPref = mActivity.getPreferences(Context.MODE_APPEND); 
     SharedPreferences.Editor editor = sharedPref.edit(); 
     editor.putLong(dataLastDownloadKey, numberLong); 
     boolean commit = editor.commit(); 
    } 

    public static long readLastDownload(Activity mActivity, long defaultValue) { 
     SharedPreferences sharedPref = mActivity.getPreferences(Context.MODE_PRIVATE); 
     return sharedPref.getLong(dataLastDownloadKey, defaultValue); 
    } 

我從com.example1試圖saveDownloadreadLastDownload,但我還沒有得到這些共同的喜好任意值。我怎樣才能以現代的方式讀取這個值(沒有任何安全漏洞,儘管我不想讓它變得私密)。

我該怎麼辦?

非常感謝您提前。

回答

2

如果你想與喜好來做到這一點:MODE_WORLD_READABLE

創建全球可寫的文件是很危險的,容易造成 安全漏洞的應用程序。強烈不鼓勵;相反, 應用程序應使用更正式的交互機制,如 ContentProvider,BroadcastReceiverService

+0

Hello Elenasys。 我問的原因是因爲MODE_WORLD_READABLE從> = API 17已棄用,是不是有其他方式? –

+0

Hola Rafael,我使用BroadcastReceiver在每個應用程序的偏好設置中發送數據並保存值。 – Jorgesys

+0

所以我想這是唯一的選擇 –