如何在DownloadManager完成下载后自动将信息传递给接收器?

时间:2013-02-19 17:33:32

标签: android broadcastreceiver

我正在从这个ShowSingleSceneActivity下载一个zip文件,我希望在文件完成后自动解压缩。所以我在这个活动中创建了一个处理程序和接收器来执行解压缩作业。但是,我的问题是,当我按下后退按钮时,解压缩将停止,我该如何解决问题?

这里是ShowSingleSceneActivity

public class ShowSingleSceneActivity extends Activity{
    public static String ZIP_FOLDER = "3dModelsZipped";
    public static String UNZIPPED_FOLDER = "3dModelsUnzipped";
    private Context context;
    BroadcastReceiver onComplete;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        context = this.getApplicationContext();
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.scene_detail);
        Intent in = this.getIntent();
        final String title = in.getStringExtra("title");
        String pic = in.getStringExtra("pic");
        String desc = in.getStringExtra("desc");
        final String model = in.getStringExtra("model");
        ImageLoader imageLoader = new ImageLoader(this);

        TextView sTitle = (TextView) findViewById(R.id.scene_title);
        TextView sDesc = (TextView) findViewById(R.id.scene_desc);
        Button sButton = (Button) findViewById(R.id.scene_button);
        ImageView sImage = (ImageView) findViewById(R.id.scene_pic);


        Handler compressHandler;
        HandlerThread handlerThread = new HandlerThread("ht"); 
        handlerThread.start();
        Looper looper = handlerThread.getLooper();
        compressHandler = new Handler(looper);

        onComplete=new BroadcastReceiver() {
                public void onReceive(Context ctxt, Intent intent) {
                    Log.d("ctxt.getExternalFilesDir(null)", "inside onComplete"+ctxt.getExternalFilesDir(null));
                    String destination = ctxt.getExternalFilesDir(null)+ "/" + UNZIPPED_FOLDER +"/";
                    String zipFile = ctxt.getExternalFilesDir(null) + "/"+ZIP_FOLDER +"/" +title+".zip";


                    Decompress myDecompress = new Decompress(zipFile, destination); 
                    myDecompress.unzip();
                }
              };

        // Setting all values in listview

        context.registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE), null, compressHandler);
        imageLoader.displayImage(pic, sImage,200);   

        sTitle.setText(title);

        sDesc.setText(desc);
        sButton.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //final DownloadThread myThread = new DownloadThread(model);
                MyDownloadManager dm = new MyDownloadManager(context, model, title);

                dm.startDownload();

            }});
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        this.getApplicationContext().unregisterReceiver(onComplete);

    }

}

0 个答案:

没有答案