有背景任务的飞溅屏幕

时间:2014-03-03 11:52:13

标签: android splash-screen

我有一个启动画面,可以从内部存储加载URL并从Web下载其内容(使用AsynkTask)。它将下载的数据放入ArrayList,调用主Activity并完成。主活动适配器管理ArrayList并设置包含其数据的ListView 当我在主Activity时,如果按下后退按钮,应用程序退出(我为启动画面活动设置了android:nohistory =“true”),但是当我返回应用程序时,启动画面被加载并且再次下载数据,“加倍”列表视图 当我返回应用程序时,如何防止加载启动画面?

启动屏幕代码:

Context mContext;
ProgressBar progress = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = this;
    setContentView(R.layout.activity_launcher);

    progress = (ProgressBar)findViewById(R.id.progress);
    progress.setIndeterminate(true);


    if(canWriteOnExternalStorage()) {

        try {
            setupStorage();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    else 
    //dialog appears


}

AsynkTask代码:

private class LoadGames extends
AsyncTask<String, Integer, Boolean> {

    private ProgressDialog mProgressDialog = null;
    private String remoteUrl = null;

    @Override
    protected void onCancelled() {
        Log.e(com.example.ludos2_0.MainActivity.TAG,
                "AsyncTask->LoadGames: onCancelled !");
        super.onCancelled();
    }

    @Override
    protected void onPreExecute() {
        Log.d(com.example.ludos2_0.MainActivity.TAG,
                "AsyncTask->LoadGames: onPreExecute !");

    }

    @Override
    protected Boolean doInBackground(String... params) {

        if (params.length == 0)
            return false;
        else
            for (int k = 0; k < (params.length)/2; ++k)
            {
                this.remoteUrl = params[k*2];

                Log.d(com.example.ludos2_0.MainActivity.TAG,
                        "AsyncTask->LoadGames: doInBackground ! ("
                                + this.remoteUrl + ")");

                // HTTP Request to retrieve the videogames list in JSON format
                try {

                    // Creates the remote request
                    Log.d(com.example.ludos2_0.MainActivity.TAG,
                            this.remoteUrl);
                    RESTRequest request = new RESTRequest(this.remoteUrl);
                    request.isMethodGET(true);

                    // Executes the request and print the received response
                    String response = RESTRequestExecutor.execute(request);

                    // Custom/Manual parsing using GSON
                    JsonParser parser = new JsonParser();

                    if (response != null && response.length() > 0) {
                        Log.d(com.example.ludos2_0.MainActivity.TAG, "Response: "
                                + response);
                        JsonObject jsonObject = (JsonObject) parser.parse(response);

                        JsonObject itemObj = jsonObject.getAsJsonObject("results");

                        String id = null;
                        String title = null;
                        String thumbnail = null;
                        String description = null;
                        String image = null;
                        String platform = null;

                            id = itemObj.get("id").getAsString();
                            title = itemObj.get("name").getAsString();

                            if (!(itemObj.get("image").isJsonNull()))
                            {
                                thumbnail = ((JsonObject)itemObj.get("image")).get("tiny_url").getAsString();
                                image = ((JsonObject)itemObj.get("image")).get("small_url").getAsString();
                            }
                            else 
                            {
                                thumbnail = "http://www.persicetometeo.com/images/not_available.jpg";
                                image = "http://www.persicetometeo.com/images/not_available.jpg";
                            }
                            description = itemObj.get("deck").getAsString();        

                            platform = params[k*2 + 1];

                            Log.d(com.example.ludos2_0.MainActivity.TAG,
                                    title);

                            ListsManager.getInstance().addVideogame(new Videogame(id, title, thumbnail, image, description, platform));



                    } else {
                        Log.d(com.example.ludos2_0.MainActivity.TAG,
                                "Error getting response ...");
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                    Log.e(com.example.ludos2_0.MainActivity.TAG,
                            "Exception: " + e.getLocalizedMessage());
                }

            }
        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        Log.d(com.example.ludos2_0.MainActivity.TAG,
                "AsyncTask->LoadGames: onPostExecute !");

        progress.setVisibility(View.GONE);

        if (result == false) {
            Log.e(com.example.ludos2_0.MainActivity.TAG,
                    "AsyncTask->LoadGames: Error Downloading Data !");
        } else {
            Log.d(com.example.ludos2_0.MainActivity.TAG,
                    "AsyncTask->LoadGames: Data Correctly Downloaded !");
            Intent intent = new Intent(mContext, MainActivity.class);
            startActivity(intent);
            finish();
        }

        super.onPostExecute(result);
    }
}

setupStorage()方法从Storage加载文件并执行AsynkTask 也许onRestart()方法的覆盖可能是一个解决方案吗? 或者我应该阻止AsyncTask加载已经下载的数据吗? 谢谢!

1 个答案:

答案 0 :(得分:1)

最好阻止AsynkTask再次下载。或者更好地清除列表视图数据。意味着如果将ArrayList与List适配器一起使用,则只需在存储新数据之前清除它。