下载管理器下载文件

时间:2014-04-01 15:40:37

标签: android android-intent android-service download-manager android-download-manager

大家好我是Android开发的新手。我想使用Download Manager下载pdf文件。我希望它一旦下载,我可以通过通知打开它。我怎样才能做到这一点。我有网址。我不知道我在哪里做错了。 以下是我尝试过的代码:

import in.my.app.constant.BConstant;
import in.my.app.databasemanagement.SDatabaseHandler;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;
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.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class AnnouncementDetailsActivity extends Activity implements
    OnClickListener {
private static String nid;
private static String announcementTitle;
private static String announcementDetails;
private static String announcementCreatedDate;
private static String cookie;
private static String token;
SharedPreferences sharedPreferences;
TextView title;
TextView details;
TextView createdDate;
TextView createdMonth;
Typeface tf;
Typeface announcementDetailsFont;
ArrayList<String> returnsAnnouncementFilesids = new ArrayList<String>();
SDatabaseHandler sDatabaseHandler;
Button downloadFile;

@SuppressLint("SimpleDateFormat")
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.announcement_details_page);
    downloadFile = (Button) findViewById(R.id.get_file);
    tf = Typeface.createFromAsset(this.getAssets(),
            BConstant.FONTS_GOTHAM_BOLD_WEBFONT);
    sDatabaseHandler = new SDatabaseHandler(this);
    announcementDetailsFont = Typeface.createFromAsset(this.getAssets(),
            BConstant.FONTS_GOTHAM_BOOK_WEBFONT);

    sharedPreferences = getSharedPreferences(
            BConstant.B_LOGIN_CHECK, BConstant.PRIVATE_MODE);
    cookie = sharedPreferences.getString(
            BConstant.WEB_SERVICES_COOKIES, "");
    token = sharedPreferences.getString(BConstant.TOKEN, "");
    Intent intent = getIntent();

    nid = intent.getStringExtra(BConstant.PRODUCT_NODE_ID);
    announcementTitle = intent
            .getStringExtra(BConstant.ANNOUNCEMENT_TITLE);
    announcementDetails = intent
            .getStringExtra(BConstant.ANNOUNCEMENT_DETAILS);
    announcementCreatedDate = intent
            .getStringExtra(BConstant.CREATED_DATE);
    returnsAnnouncementFilesids = sDatabaseHandler
            .getAnnouncementURLsFID(nid);

    createdDate = (TextView) findViewById(R.id.getAnnouncementDate);
    title = (TextView) findViewById(R.id.getAnnouncementName);
    details = (TextView) findViewById(R.id.getAnnouncementDetails);
    createdMonth = (TextView) findViewById(R.id.getMonth);
    title.setTypeface(tf);
    details.setTypeface(announcementDetailsFont);

    long cDate = Long.parseLong(announcementCreatedDate);
    final Calendar c = Calendar.getInstance();
    c.setTimeInMillis(cDate * 1000L);
    Date d = c.getTime();
    SimpleDateFormat simpleDateformat = new SimpleDateFormat("dd");
    SimpleDateFormat simpleMonthformat = new SimpleDateFormat("MMM");

    String date = simpleDateformat.format(d);
    String month = simpleMonthformat.format(d);

    createdDate.setText(date);
    createdMonth.setText(month);
    title.setText(announcementTitle);
    details.setText(Html.fromHtml(announcementDetails));
    downloadFile.setOnClickListener(this);

}

@Override
public void onBackPressed() {
    super.onBackPressed();
    overridePendingTransition(R.anim.right_in_details,
            R.anim.right_out_left_in_details);
}

BroadcastReceiver onComplete=new BroadcastReceiver() {
    public void onReceive(Context ctxt, Intent intent) {

    }
};
@Override
public void onClick(View v) {

Intent intent = getIntent();
    DownloadManager downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
    Uri Download_Uri = Uri.parse(sDatabaseHandler.getAnnouncementURL(
            returnsAnnouncementFilesids.get(0)).get(0));
    DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
    request.addRequestHeader(BConstant.WEB_SERVICES_COOKIES, cookie);
    request.addRequestHeader(BConstant.WEB_SERVICES_TOKEN_HEADER, token);
    request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
    request.setAllowedOverRoaming(false);
    request.setTitle(intent.getStringExtra("mydownload"));
    request.setDestinationInExternalFilesDir(this,Environment.DIRECTORY_DOWNLOADS,"test" + ".gpg");
    Long downloadReference = downloadManager.enqueue(request);
    registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

}
 }

编辑:

04-01 22:18:51.172: E/ActivityThread(705): Activity in.bsharp.app.AnnouncementDetailsActivity has leaked IntentReceiver in.bsharp.app.AnnouncementDetailsActivity$1@42b1b230 that was originally registered here. Are you missing a call to unregisterReceiver()?
04-01 22:18:51.172: E/ActivityThread(705): android.app.IntentReceiverLeaked: Activity in.bsharp.app.AnnouncementDetailsActivity has leaked IntentReceiver in.bsharp.app.AnnouncementDetailsActivity$1@42b1b230 that was originally registered here. Are you missing a call to unregisterReceiver()?
04-01 22:18:51.172: E/ActivityThread(705):  at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:792)
04-01 22:18:51.172: E/ActivityThread(705):  at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:593)
04-01 22:18:51.172: E/ActivityThread(705):  at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1225)
04-01 22:18:51.172: E/ActivityThread(705):  at android.app.ContextImpl.registerReceiver(ContextImpl.java:1212)
04-01 22:18:51.172: E/ActivityThread(705):  at android.app.ContextImpl.registerReceiver(ContextImpl.java:1206)
04-01 22:18:51.172: E/ActivityThread(705):  at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:376)
04-01 22:18:51.172: E/ActivityThread(705):  at in.bsharp.app.AnnouncementDetailsActivity.onClick(AnnouncementDetailsActivity.java:158)
04-01 22:18:51.172: E/ActivityThread(705):  at android.view.View.performClick(View.java:4191)
04-01 22:18:51.172: E/ActivityThread(705):  at android.view.View$PerformClick.run(View.java:17229)
04-01 22:18:51.172: E/ActivityThread(705):  at android.os.Handler.handleCallback(Handler.java:615)
04-01 22:18:51.172: E/ActivityThread(705):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-01 22:18:51.172: E/ActivityThread(705):  at android.os.Looper.loop(Looper.java:137)
04-01 22:18:51.172: E/ActivityThread(705):  at android.app.ActivityThread.main(ActivityThread.java:4960)
04-01 22:18:51.172: E/ActivityThread(705):  at java.lang.reflect.Method.invokeNative(Native Method)
04-01 22:18:51.172: E/ActivityThread(705):  at java.lang.reflect.Method.invoke(Method.java:511)
04-01 22:18:51.172: E/ActivityThread(705):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
04-01 22:18:51.172: E/ActivityThread(705):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
04-01 22:18:51.172: E/ActivityThread(705):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

Activity has leaked IntentReceiver that was originally registered here.
Are you missing a call to unregisterReceiver()?

尝试在代码中添加unregisterReceiver方法。例如,重写OnPause方法并在那里插入插入代码。

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

            unregisterReceiver(br);
        }