2015-10-30 30 views
0

我有一個應用程序,它在列表視圖中顯示通知。我希望保存這些通知,這樣如果我打開應用程序並看到通知,我可以在關閉應用程序然後打開它時再次看到這些通知。我試過this 但沒有保存。使用SavePreference保存ListView項目Android

另一個主要問題是我如何在後臺運行這個應用程序?因此,如果收到通知,應用程序會在列表視圖中列出該通知而不打開?

我的代碼

public class MainActivity extends Activity { 

    ListView list; 
    CustomListAdapter adapter; 
    ArrayList<Model> modelList; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     modelList = new ArrayList<Model>(); 
     adapter = new CustomListAdapter(getApplicationContext(), modelList); 
     list=(ListView)findViewById(R.id.list); 
     list.setAdapter(adapter); 
     LocalBroadcastManager.getInstance(this).registerReceiver(onNotice, new IntentFilter("Msg")); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_main, menu);//Menu Resource, Menu 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.action_settings: 
       Intent intent = new Intent(
         "android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"); 
       startActivity(intent); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 
    private BroadcastReceiver onNotice= new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      String pack = intent.getStringExtra("package"); 
      String title = intent.getStringExtra("title"); 
      String text = intent.getStringExtra("text"); 
      //int id = intent.getIntExtra("icon",0); 

      Context remotePackageContext = null; 
      if (pack.contains("fake")){ 

      try { 
//    remotePackageContext = getApplicationContext().createPackageContext(pack, 0); 
//    Drawable icon = remotePackageContext.getResources().getDrawable(id); 
//    if(icon !=null) { 
//     ((ImageView) findViewById(R.id.imageView)).setBackground(icon); 
//    } 
       byte[] byteArray = intent.getByteArrayExtra("icon"); 
       Bitmap bmp = null; 
       if (byteArray != null) { 
        bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 
       } 
       Model model = new Model(); 
       if(text.contains("") && !text.contains(" messages")) { 
        model.setName(title + ": " + text); 
        model.setImage(bmp); 

        if (modelList != null) { 
         modelList.add(model); 
         adapter.notifyDataSetChanged(); 
        } else { 
         modelList = new ArrayList<Model>(); 
         modelList.add(model); 
         adapter = new CustomListAdapter(getApplicationContext(), modelList); 
         list = (ListView) findViewById(R.id.list); 
         list.setAdapter(adapter); 
        } 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 


     } 
    } 
    }; 



    } 
+0

基本上你想保存modelList在SharedPrefs? – vaibhav

+0

是的,這是我想要的 –

回答

1

使類高速緩衝存儲器,有能力進行序列化和反序列化數據。

public class Cache { 

    private static Cache CACHE; 

    public static Cache get() { 
     if (!SharedPreferencesHelper.isCacheAvailable()) { 
      CACHE = new Cache(); 
      SharedPreferencesHelper.saveCache(CACHE); 
     } else { 
      CACHE = SharedPreferencesHelper.getCache(); 
     } 

     return CACHE; 
    } 

    ArrayList<Taxonomy> cachedTaxonomies; 
    public Cache() { 
     cachedTaxonomies = new ArrayList<Taxonomy>(); 
    } 
    public ArrayList<Taxonomy> getCachedTaxonomies() { 
     return cachedTaxonomies; 
    } 
    public static String serialize(Cache cache) { 
     GsonBuilder builder = new GsonBuilder(); 
     Gson gson = builder.enableComplexMapKeySerialization().setPrettyPrinting().create(); 
     return gson.toJson(cache); 
    } 

    public static Cache deserialize(String json) { 
     Type type = new TypeToken<Cache>() { 
     }.getType(); 
     return new Gson().fromJson(json, type); 
    } 

    public void update() { 
     SharedPreferencesHelper.saveCache(this); 
    } 
} 

這裏Taxonomy是一個模型。 下面是它可以幫助您在SharedPrefs保存類

public class SharedPreferencesHelper { 

    private static final String PREFS_CACHE = "prefs_cache"; 


    public static SharedPreferences getSharedPreferences() { 
     return SpreeApplication.getSharedPreferences(); 
    } 

    // Cache ------------------------------------- 
    public static boolean isCacheAvailable() { 
     SharedPreferences sharedPreferences = getSharedPreferences(); 
     String json = sharedPreferences.getString(PREFS_CACHE, ""); 
     if(json.equals("")) { 
      return false; 
     } else { 
      return true; 
     } 
    } 

    public static Cache getCache() { 
     SharedPreferences sharedPreferences = getSharedPreferences(); 
     String json = sharedPreferences.getString(PREFS_CACHE, ""); 
     if(json.equals("")) { 
      return null; 
     } else { 
      return Cache.deserialize(json); 
     } 
    } 

    public static void saveCache(Cache cache) { 
     saveString(PREFS_CACHE, Cache.serialize(cache)); 
    } 
    // ----------------------------------------------------- 

    private static void saveString(String prefKey, String value) { 
     SharedPreferences sharedPreferences = getSharedPreferences(); 
     SharedPreferences.Editor prefEditor = sharedPreferences.edit(); 
     prefEditor.putString(prefKey, value); 
     prefEditor.commit(); 
    } 

    private static void saveBoolean(String prefKey, boolean value) { 
     SharedPreferences sharedPreferences = getSharedPreferences(); 
     SharedPreferences.Editor prefEditor = sharedPreferences.edit(); 
     prefEditor.putBoolean(prefKey, value); 
     prefEditor.commit(); 
    } 
} 

要保存這樣寫:

List<Taxonomy> taxonomies = new ArrayList<Taxonomy>(); 
Cache cache = Cache.get(); 
cache.getCachedTaxonomies().clear(); 
cache.getCachedTaxonomies().addAll(taxonomies); 
SharedPreferencesHelper.saveCache(cache); 

這是我的spreeapplication類,這是一個自定義的應用程序類 記住,你必須在明顯的,如果提你創建一個自定義的應用程序類

public class SpreeApplication extends Application{ 
    private final static String DEFAULT_PREFERENCES = "spree"; 
    private static SharedPreferences sharedPreferences; 
    private static Context applicationContext; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     applicationContext = this; 
     sharedPreferences = getSharedPreferences(DEFAULT_PREFERENCES, Context.MODE_PRIVATE); 
    } 

    public static SharedPreferences getSharedPreferences() { 
     return sharedPreferences; 
    } 

    public static SharedPreferences.Editor getSharedPreferencesEditor() { 
     return sharedPreferences.edit(); 
    } 

    public static Context getContext() { 
     return applicationContext; 
    } 
} 
+0

什麼是「類型」?我應該爲它導入哪個類? –

+0

import java.lang.reflect.Type; – vaibhav

+0

什麼是SpreeApplication在這裏返回SpreeApplication.getSharedPreferences(); –