我有一個是發射時執行的異步任務:向前傳遞AsyncTask的結果?
public class BufferApps extends AsyncTask<String, Void, ArrayList<PackageInfo>> {
PackageManager mPackageManager;
Context mContext;
public BufferApps(PackageManager pm, Context context){
super();
this.mPackageManager = pm;
this.mContext = context;
}
protected ArrayList<PackageInfo> doInBackground(String... strings) {
//this code works, not where the problem is
return installedApps;
}
protected void onPostExecute(ArrayList<PackageInfo> installedApps) {
?
Toast.makeText(mContext, "Finished!", Toast.LENGTH_SHORT).show();
}
}
基本上這裏發生了什麼是我創建的對象的ArrayList,特別是通過設備上的用戶安裝的所有應用程序的列表。我想我只是在這裏畫一個空白,但我一直在嘗試這一段時間,不能完全弄明白:我傳遞給onPostExecute的installedApps
似乎沒問題。所以說installedApps
就是上面描述的列表。
我想要的是將這個列表存儲在某個地方,這樣當我需要在我的設備上安裝的應用程序列表時,我可以快速訪問它,而無需執行加載已安裝應用程序的所有艱難工作(doInBackground中的代碼需要相當長的時間來執行)。我怎樣才能做到這一點?
我已經嘗試過gson序列化與SharedPreference一起使用,由於某種原因會引發錯誤。我試圖創建一個單獨的類只有一個ArrayList對象是static
來包含這些條目,但這也似乎失敗。如果有必要,我可以提出我的嘗試,但是如果任何人都可以推薦一個好方法來存儲ArrayList,以便其他所有活動都可以輕鬆訪問它,那將是最有幫助的。
你應該看看不同選項的Android爲您提供:http://developer.android.com/guide/topics/data/data-storage.html –
您可以使用。調用AsyncTask的類中的get()方法。如... BufferApps.execute(); arrayList = BufferApps.get(); –