2012-09-12 34 views
9

Tôi có một ứng dụng trên thị trường Google Play. Vì nhiều lý do mà tôi sẽ không bận tâm, tôi đã thay đổi loại sở thích của mình. Ví dụ một kiểu tùy chọn là một số nguyên và trong phiên bản gần đây nhất nó là một chuỗi. Tôi tưởng tượng đây không phải là thực hành tốt nhưng tiếc là tôi đã phải làm điều đó.Làm cách nào để xóa các tùy chọn cũ khi cập nhật ứng dụng Android?

Mối quan tâm của tôi là khi ai đó cập nhật lên phiên bản mới, ứng dụng của họ sẽ gặp sự cố do loại tùy chọn đã thay đổi. Vì lý do này, tôi muốn xóa các sở thích bất cứ khi nào ứng dụng được cập nhật (một lần nữa tôi nhận ra không lý tưởng!)

Điều này có khả thi không?

Trả lời

14

Lớp SharedPreferences.Editor có chức năng clear(), loại bỏ tất cả các tùy chọn đã lưu trữ của bạn (sau commit()). Bạn có thể tạo cờ boolean để chỉ ra nếu cập nhật cần thiết:

void updatePreferences() { 
    SharedPreferences prefs = ...; 
    if(prefs.getBoolean("update_required", true)) { 
     SharedPreferences.Editor editor = prefs.edit(); 
     editor.clear(); 

     /*....make the updates....*/ 

     editor.putBoolean("update_required", false) 
     editor.commit(); 
    } 
} 

Và sau đó bạn cần gọi điều này trong hoạt động chính (bắt đầu lần đầu) trước khi bạn truy cập bất kỳ người thích nào.

EDIT

Để có được phiên bản hiện tại (Các mã phiên khai báo trong manifest):

int version = 1; 
try { 
    version = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode; 
} catch (NameNotFoundException e) { 
    e.printStackTrace(); 
} 

if(version > ...) { 
    //do something 
} 

EDIT

Nếu bạn muốn làm một số thao tác cập nhật, bất cứ khi nào thay đổi phiên bản, sau đó bạn có thể làm điều gì đó như sau:

void runUpdatesIfNecessary() { 
    int versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode; 
    SharedPreferences prefs = ...; 
    if (prefs.getInt("lastUpdate", 0) != versionCode) { 
     try { 
      runUpddates(); 

      // Commiting in the preferences, that the update was successful. 
      SharedPreferences.Editor editor = prefs.edit(); 
      editor.putInt("lastUpdate", versionCode); 
      editor.commit(); 
     } catch(Throwable t) { 
      // update failed, or cancelled 
     } 
    } 
} 
+0

Có thể cho biết thời điểm người dùng quyết định cập nhật từ thị trường Google Play không? – Mel

+0

Đó là chính xác những gì mã này làm: nếu phiên bản sẽ là mới thì bạn sẽ biết rằng người dùng quyết định làm một bản cập nhật – Phate

+0

Nó không phải là một cách thực sự đẹp để làm việc, nhưng bạn cũng có thể bắt đầu lưu trữ phiên bản hiện tại sharedpreferences và khi nó không khớp với phiên bản ứng dụng thực tế của bạn (được hiển thị ở trên, cách tải xuống), bạn cũng có thể làm điều gì đó. –

1

Bạn cũng có thể thêm số phiên bản trong khi nhận được SharedPreferences như thế này

context.getSharedPreferences("MyKey" + app_version_no, Context.MODE_PRIVATE); 

Vì vậy, nếu bạn cập nhật ứng dụng, số phiên bản sẽ thay đổi và bạn sẽ có một tập mới của các sở thích.

+0

Điều này sẽ dẫn đến việc lưu trữ dữ liệu ngày càng tăng .. – baash05

0

Mã cửa hàng phiên bản trong SharedPreferences mặc định và lưu trữ các sở thích khác của bạn trong phiên bản quy định hồ sơ, khi sự thay đổi phiên bản bạn có thể tìm thấy cũ mã phiên bản của bạn và bạn có thể có tập tin sở thích cũ của bạn và cập nhật phiên bản mới bằng cách dữ liệu cũ

int versionCode=0,oldVersion; 
    try 
    { 
     versionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode; 
    } 
    catch (PackageManager.NameNotFoundException e) 
    {} 
    SharedPreferences prefs =getSharedPreferences("MyPref" + versionCode, Context.MODE_PRIVATE); 
    SharedPreferences prefsDefault = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); 


    oldVersion=prefsDefault.getInt("OldVersion",0); 


    if (oldVersion!=versionCode) 
    { 
     if (oldVersion>0) 
     { 
      // ToDo Update Preferences 
      String oldPrefsKey="MyPref" + oldVersion; 
      SharedPreferences oldPrefs =context.getSharedPreferences(oldPrefsKey, Context.MODE_PRIVATE); 
      SharedPreferences.Editor pEdit=prefs.edit(); 
      pEdit.putBoolean("clock24",oldPrefs.getBoolean("clock24",false)); 
      pEdit.putBoolean("show_clock",oldPrefs.getBoolean("show_clock",true)); 
      pEdit.commit(); 
      oldPrefs.edit().clear().commit(); 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
       deleteSharedPreferences(oldPrefsKey); 
      } else { 
       try { 
        File oldFile=new File(getCacheDir().getParent() + "/shared_prefs/"+oldPrefsKey+".xml"); 
        oldFile.delete(); 
       } catch (Exception e) { 

       } 
      } 

     } 
     prefsDefault.edit().putInt("OldVersion",versionCode).commit(); 




    } 
0
final String PREFERENCE_NAME = "my_preference"; 

final String APP_VERSION_CODE = 6; /*change this each time you want to clear 
preference in updated app.*/ 

preferences = getSharedPreferences(PREFERENCE_NAME, MODE_PRIVATE); 

if (preferences.getInt(PREFERENCE_VERSION, 0) != APP_VERSION_CODE) { 
     preferences.edit().clear().apply(); 
     preferences.edit().putInt(PREFERENCE_VERSION, APP_VERSION_CODE).apply(); 
} 
Các vấn đề liên quan