我有一个是发射时执行的异步任务:向前传递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(); –