当前位置: 动力学知识库 > 问答 > 编程问答 >

Issue with Download Manager Android 4.4.2

问题描述:

I had written a Android app with SDK 4.4.2, Eclipse 4.2.2 Juno.

The main functionality of the app display a button "Click to Download" and when some one click on the button it will check if the file is already downloading by querying Android Download Manager. If not it will download a file from local server using Android Download Manager. If yes it will display a toast message that the file download is in progress.

The app works perfectly, but fails in one case as follows.

  1. Install the app and run it.
  2. Click on the button "Click to Download".
  3. App Queries the Download Manager and starts download.
  4. Now exit the app and uninstall it. ( Download still in progress)
  5. Install the app again and run it. ( Download still in progress)
  6. Click on the button "Click to Download", it will initiate a new download. ( Download still in progress + New Download of the same file)

On step 6 Download Manager fails to return the file download status and this fails only for SDK 4.4.2. The app works perfectly well from API 8 to API 18 and the above case fails only on API 19.

I have tested this on Moto G with KitKat 4.4.2.

Will share the code if some one wants to test, it below 100 lines

Conditions:

  1. I already know the file name i am downloading and query using the file name.
  2. The file is about 100MB and i have plenty of time to uninstall and reinstall my app before the download finishes.

Code:

MainActivity.java

package com.test.downloadprogress;

import android.app.Activity;

import android.app.DownloadManager;

import android.app.DownloadManager.Query;

import android.app.DownloadManager.Request;

import android.content.BroadcastReceiver;

import android.database.Cursor;

import android.net.Uri;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

public long enqueue;

private DownloadManager dm;

BroadcastReceiver receiver;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button button1 =(Button)findViewById(R.id.button1);

button1.setOnClickListener(this);

}

public void onClick(View view) {

dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);

/*Check if we had already initiated the download, if YES we display a Toast Message or

* we make the Download Manager to handle the request*/

if(!isDownloading("file.txt"))

{

Request request = new Request(

Uri.parse("http://YOUR_LOCAL_IP_HERE/dtest/file.txt"));

this.enqueue = dm.enqueue(request);

Log.i("DP", "DownloadID:::"+ this.enqueue);

}

else

Toast.makeText(MainActivity.this, "Downloading.....", Toast.LENGTH_LONG).show();

}

/* Query Download Manager and return the Download Status of the file*/

public boolean isDownloading(String dfilename)

{

boolean isRunning = false;

try {

Query query = new Query();

query.setFilterByStatus(DownloadManager.STATUS_RUNNING | DownloadManager.STATUS_PAUSED | DownloadManager.STATUS_PENDING);

Cursor c = dm.query(query);

Log.i("DP", "CursorCount:::"+c.getCount());

for(c.moveToFirst();!c.isAfterLast(); c.moveToNext())

{

int columnIndex = c

.getColumnIndex(DownloadManager.COLUMN_TITLE);

String fileName=c.getString(columnIndex);

Log.i("DP:", "FileName:::"+fileName);

if(fileName.equals(dfilename))

{

isRunning=true;

break;

}

}

c.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return isRunning;

}

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity" >

<Button

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentTop="true"

android:layout_marginLeft="46dp"

android:layout_marginTop="138dp"

android:text="@string/download" />

</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.test.downloadprogress"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk

android:minSdkVersion="8" />

<uses-permission android:name="android.permission.INTERNET"/>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name="com.test.downloadprogress.MainActivity"

android:label="@string/app_name" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

</application>

</manifest>

分享给朋友:
您可能感兴趣的文章:
随机阅读: