onPostExecute有时只在AsyncTask中调用

时间:2016-04-01 18:09:03

标签: java android android-asynctask

我的AsyncTask在我的MainActivity中执行,但它不调用onPostExecute。方法doInBackground完成,直到return语句(使用System.out检查)!

电话:

@Override
protected void onStart() {
    super.onStart();

    if (playIntent == null) {
        playIntent = new Intent(this, MusicService.class);

        if (CheckPermissions()) {
            Prepare();
        }
    }
}

private void Prepare() {
    MusicStore musicStore = new MusicStore(getApplicationContext());

    sSongs = musicStore.getSongs();

    StoreParcel storeParcel = new StoreParcel(StoreParcel.StoreAction.READ_PLAYLISTS, this);

    musicStore.execute(storeParcel);

    if (sSongs.length < 1) {
        fabPlayPause.hide();

        snbInformation = Snackbar.make(recyclerView, getString(R.string.snb_Information) + "Music.", Snackbar.LENGTH_INDEFINITE);
        snbInformation.show();
    }
    else {
        fabPlayPause.show();
    }

    bindService(playIntent, musicServiceConn, BIND_AUTO_CREATE);

    startService(playIntent);
}

AsyncTask:

public class MusicStore extends AsyncTask<StoreParcel, Void, StoreParcel> {
    private Context mContext;

    public MusicStore(Context context) {
        mContext = context;
    }

    //region AsyncTask

    @Override
    protected StoreParcel doInBackground(StoreParcel... params) {
        StoreParcel parcel = params[0];

        StoreParcel storeParcel = new StoreParcel(parcel.getAction(), parcel.getPlaylistInterface());

        switch (parcel.getAction()) {
            case WRITE_PLAYLISTS:
                WritePlaylists(parcel.getPlaylists());
                break;
            case READ_PLAYLISTS:
                storeParcel.setPlaylists(ReadPlaylists());
                break;
        }

        return storeParcel;
    }

    @Override
    protected void onPostExecute(StoreParcel storeParcel) {
        if (storeParcel.getAction() == StoreParcel.StoreAction.READ_PLAYLISTS) {
            storeParcel.getPlaylistInterface().SyncPlaylists(storeParcel.getPlaylists());
        }

        super.onPostExecute(storeParcel);
    }

    //region Methods

    private void WritePlaylists(Playlist[] playlists) {
        File dir = new File(mContext.getFilesDir() + Preferences.dirPlaylists);

        if (!dir.exists()) {
            dir.mkdirs();
        }

        for (File f : dir.listFiles()) {
            f.delete();
        }

        if (playlists == null) return;

        String sFilename;
        File file;

        for (int i = 0; i < playlists.length; i++) {
            sFilename = playlists[i].getName();

            try {
                file = new File(dir, sFilename + ".json");
                file.createNewFile();

                Writer writer = new OutputStreamWriter(new FileOutputStream(file));

                Gson gson = new GsonBuilder().create();
                gson.toJson(playlists[i], writer);

                    writer.close();
            }
            catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }

    private Playlist[] ReadPlaylists() {
        Playlist[] playlists;

        File dir = new File(mContext.getFilesDir() + Preferences.dirPlaylists);

        File[] files = dir.listFiles();

        if (files == null) return null;

        playlists = new Playlist[files.length];

        Reader reader = null;

        try {
            for (int i = 0; i < files.length; i++) {
                reader = new InputStreamReader(new FileInputStream(files[i]));

                Gson gson = new GsonBuilder().create();
                playlists[i] = gson.fromJson(reader, Playlist.class);
            }

            if (reader != null) reader.close();
        }
        catch (IOException ioe) {
            ioe.printStackTrace();
        }

        return playlists;
    }

    //endregion

    //endregion
}

StoreParcel是我创建的一个类!它只计算一个接口和一个枚举值!

1 个答案:

答案 0 :(得分:0)

我认为你应该在onResume中运行任务,这样你才能存在。您可以在onStart中执行UI内容,并且onPostExecute必须在UI线程上运行。

@Override
protected void onStart() {
  super.onStart();

  if (playIntent == null) {
    playIntent = new Intent(this, MusicService.class);
  }
}

@Override
protected void onStart() {
    super.onStart();
    if (CheckPermissions()) {
        Prepare();
    }
}