簡體   English   中英

Android:相同的代碼從一天到另一天的行為發生了變化

[英]Android: Same code has change behaviour from one day to an other

我很震驚。 我使用基於 Android Recipes 4th Edition 一書的代碼片段來下載我需要的 zip 文件代碼。 今天,我更新了那個 zip 文件,突然間,我注意到我的代碼不起作用(這應該沒有意義,因為服務器端和客戶端不相關)

好吧,我開始調試,甚至嘗試重新制作代碼段,但情況是一樣的,Activity沒有做部分代碼,沒有進入onResume。

有誰知道它可能是什么? 謝謝。

    package com.example.juanse.secgps;

import android.app.Activity;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;

import net.lingala.zip4j.core.ZipFile;

public class DescargaCiudad extends Activity {
    private static final String DL_ID = "downloadId";
    private SharedPreferences prefs;
    private DownloadManager dm;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        prefs = PreferenceManager.getDefaultSharedPreferences(this);
        dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!prefs.contains(DL_ID)) {
//Start the download
            Uri resource = Uri.parse("http://www.adeter.org/zipSample.zip");
            DownloadManager.Request request = new DownloadManager.Request(resource);
//Set allowed connections to process download
            request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE
                    | DownloadManager.Request.NETWORK_WIFI);
            request.setAllowedOverRoaming(false);
//Finding a place to be saved

            request.setDestinationInExternalPublicDir("/omw", "zipSample.zip");

//Display in the notification bar
            request.setTitle("Download Sample");
            long id = dm.enqueue(request);
//Save the unique id
            prefs.edit().putLong(DL_ID, id).commit();
        } else {
            //Download already started, check status
            queryDownloadStatus();
        }
        registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }

    @Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(receiver);
    }

    private BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            queryDownloadStatus();
        }
    };

    private void queryDownloadStatus() {
        DownloadManager.Query query = new DownloadManager.Query();
        query.setFilterById(prefs.getLong(DL_ID, 0));
        Cursor c = dm.query(query);

        if (c.moveToFirst()) {
            int status = c.getInt(
                    c.getColumnIndex(DownloadManager.COLUMN_STATUS));
            switch (status) {
                case DownloadManager.STATUS_PAUSED:
                case DownloadManager.STATUS_PENDING:
                case DownloadManager.STATUS_RUNNING:
//Do nothing, still in progress
                    break;
                case DownloadManager.STATUS_SUCCESSFUL:
//Done, display the image
                    try {
                        //bajado con e
                        String ZipFileLocation = "/sdcard/omw/zipSample.zip";
                        String unzipLocation = "/sdcard/omw/";


                        // Initiate ZipFile object with the path/name of the zip file.
                        ZipFile zipFile = new ZipFile(ZipFileLocation);
                        // Extracts all files to the path specified
                        zipFile.extractAll(unzipLocation);
                        // Call the map main activity to draw the points
                        Intent i = new Intent(getApplicationContext(), MainMap.class);
                        startActivity(i);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case DownloadManager.STATUS_FAILED:
//Clear the download and try again later
                    dm.remove(prefs.getLong(DL_ID, 0));
                    prefs.edit().clear().commit();
                    break;
            }
        }
    }



}

我認為您的首選項已經有了 DL_ID,所以如果您從下載管理器中刪除了之前下載的文件,您將無法執行任何部分。 檢查一次

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM