在共享首选项中保存下载状态

时间:2013-04-01 20:56:41

标签: android download sharedpreferences

完全下载文件后,在共享首选项中保存下载状态的最佳做法是什么?

我的问题类似于Dealing with incomplete downloaded file,但我找不到最佳方法。

我想保存该文件已完全下载到我的asynctask onPostExecute中,例如:

protected void onPostExecute(Void result) {
    mDownloadPrefs.setDownloadState(item.FileName());
}

但是......当我尝试获取下载状态时,应用程序关闭:

Boolean exist = mDownloadPrefs.getDownloadState(item.FileName());
if(exist){
    ...

我认为偏好文件看起来像这样:

  • FileName001 = true
  • FileName002 = true
  • FileName003 = true

DownloadPreferences类:

public class DownloadPreferences {

    private static final String APP_SHARED_PREFS = DownloadPreferences.class.getSimpleName(); //  Name of the file -.xml
    private SharedPreferences _sharedPrefs;
    private Editor _prefsEditor;

    public DownloadPreferences(Context context) {
        this._sharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Activity.MODE_PRIVATE);
        this._prefsEditor = _sharedPrefs.edit();
    }

    public Boolean getDownloadState(String name) {
        boolean isdownload = _sharedPrefs.getBoolean(name, false);
        return isdownload;
    }

    public void setDownloadState(String name) {
        _prefsEditor.putBoolean(name, true);
        _prefsEditor.commit();
    }
}

Updade:Logcat结果:

AndroidRuntime: Shutting down VM
dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40f1c300)
AndroidRuntime:java.lang.NullPointerException
AndroidRuntime:     at com.myapp.reader.AdapterReader.getView(AdapterReader.java:145)
AndroidRuntime:     at android.widget.AbsListView.obtainView(AbsListView.java:2267)
AndroidRuntime:     at android.widget.GridView.onMeasure(GridView.java:1030)
AndroidRuntime:     at android.view.View.measure(View.java:15172)
AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
AndroidRuntime:     at android.view.View.measure(View.java:15172)
AndroidRuntime:     at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
AndroidRuntime:     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
AndroidRuntime:     at android.view.View.measure(View.java:15172)
AndroidRuntime:     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
AndroidRuntime:     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
AndroidRuntime:     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
AndroidRuntime:     at android.view.View.measure(View.java:15172)
AndroidRuntime:     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
AndroidRuntime:     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
AndroidRuntime:     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
AndroidRuntime:     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
AndroidRuntime:     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
AndroidRuntime:     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
AndroidRuntime:     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
AndroidRuntime:     at android.view.Choreographer.doFrame(Choreographer.java:525)
AndroidRuntime:     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:615)
AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:92)
AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:4745)
AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:511)
AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

由于

0 个答案:

没有答案