我用SDK 4.4.2编写了一个Android应用程序,Eclipse 4.2.2 Juno。
该应用程序的主要功能显示一个按钮"点击下载"当有人点击按钮时,它会通过查询Android下载管理器来检查文件是否已经下载。如果没有,它将使用Android下载管理器从本地服务器下载文件。如果是,它将显示文件下载正在进行的Toast消息。
该应用程序运行良好,但在一种情况下失败如下。
在步骤6中,下载管理器无法返回文件下载状态,这仅对SDK 4.4.2失败。从API 8到API 18,该应用程序运行良好,上述情况仅在API 19上失败。
我已经使用KitKat 4.4.2在Moto G上进行了测试。
如果某人想要测试,它将共享代码,它低于100行
条件:
代码:
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>