在我的应用程序中,我有4个按钮,当用户点击任何按钮时,它开始下载文件,进度条显示在通知区域。下载和进度条工作正常,但我有以下两个问题
- 当下载完成时,进度条没有关闭,它仍然在通知区域
- 正如我上面所说的,我有4个按钮,当第一个按钮被点击下载开始,当其他三个按钮被点击立即下载不发生。我想它可能会在第一次下载完成后开始。但是什么也没发生。如何在点击所有按钮时显示所有进度条
以下是我的代码(这里我只添加了2个按钮)请帮助我
b1 = (Button)findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
i =1;
Intent intent = new Intent(NotificationProgressTestActivity.this, UploadService.class);
startService(intent);
}
});
b2 = (Button)findViewById(R.id.button2);
b2.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
i = 2;
Intent intent = new Intent(NotificationProgressTestActivity.this, UploadService.class);
startService(intent);
}
});
下一个是我的上传服务。class
public class UploadService extends IntentService
{
private NotificationManager notificationManager;
private Notification notification;
private int progress = 10;
private static String fileName = "folder/";
private static URL url;
public UploadService(String name)
{
super(name);
}
public UploadService()
{
super("UploadService");
}
@Override
protected void onHandleIntent(Intent intent)
{
notificationManager = (NotificationManager) getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification = new Notification(R.drawable.icon,"Uploading file", System.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.upload_progress_bar);
notification.contentIntent = contentIntent;
notification.contentView.setProgressBar(R.id.progressBar1, 100, progress, false);
notificationManager.notify(42, notification);
notificationManager.notify(42, notification);
Thread download = new Thread()
{
@Override
public void run()
{
Log.e("download", "start");
try
{
for (int i = 1; i < 100; i++)
{
progress++;
notification.contentView.setProgressBar(R.id.progressBar1, 100, progress, false);
if(i==1)
{
if(NotificationProgressTestActivity.i ==1 )
{
url = new URL("http://xxxxxxxxxxxxxxxx.mp4");
}
else if(NotificationProgressTestActivity.i == 2)
{
url = new URL("http://xxxxxxxxxxxxxxxx.mp4");
}
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getExternalStorageDirectory()+ "/";
Log.e("PATH:", PATH);
File file = new File(PATH);
if (!file.exists())
{
file.mkdir();
Log.e("destination", "created");
}
else
{
Log.e("destination", "exist");
}
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[10171188];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1)
{
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
// -----------------------
if (!outputFile.exists())
{
Log.e(outputFile.toString(), "not created");
}
else
{
Log.e(outputFile.toString(), "created");
Log.e(outputFile.toString(), "" + outputFile.length());
}
Log.e("download", "end");
}
notificationManager.notify(42, notification);
try
{
Thread.sleep(1017);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
catch (IOException e)
{
Log.e("log_tag", "Error: " + e);
}
Log.e("log_tag", "Check: ");
// remove the notification (we're done)
notificationManager.cancel(42);
}
};
download.run();
}
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
1-在上面这行中,getActivity()中的第二个参数是requestCode,它应该是每个按钮的唯一编号。当前两个按钮都为0。
notificationManager.notify(42, notification);
2-在上面的行中,您传递的通知索引为42,它对于两个按钮都应该是唯一的。目前,无论你创建了多少个按钮,它都不会向你显示一个新的通知,因为你为每个按钮传递了42个。它将不断更新当前的。
你也可能需要再次查看你在onHandleIntent()中所做的代码。有更好的方法。
你需要的是一个AsyncTask
和一个ListView
。你的下载魔法发生在AsyncTask中。例如,创建一个Array,指示正在运行的下载和每个正在运行的下载的进度。现在在你的AsyncTask有一个publishProgress
-方法调用onProgressUpdate
。在onProgressUpdate
中,您必须更新各自的进度变量并在ListView适配器上调用notifyDataSetChanged
。这将导致ListView重新加载所有数据。最后在你的ListView
-适配器你有一个方法getView
,在那里你必须为你的ListView的每一行创建视图。在那里你可以决定是否显示进度条(下载正在运行)。
关于AsyncTask的更多信息可以在这里找到:http://labs.makemachine.net/2010/05/android-asynctask-example/更多关于ListView的信息在这里:Android: BaseAdapter如何?