在我的應用程序中,我有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);
}
});
下一頁下面是我Uplaod Service.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();
}
我真的很抱歉,我想到打字問題,我拼寫成s。剛纔我注意到,我將改變我的標題 –
聽起來不錯,謝謝。 :) –
爲什麼你有一個循環,如果你只是真的要在其中一個迭代中做任何真正的工作?這不會帶來有意義的進展。 –