我正在实施一个下载服务,我正在从URL下载图像。一旦服务启动,就会弹出一个通知,显示正在进行下载的进度条。进度条应该每次都更新,它应该显示"下载完成"下载完成后。但是,一旦下载完成,通知栏就会消失。此外,我无法显示进度更新。我想使用IntentService
来实现这一点。请帮忙。以下是我的代码。
public class DownloadService extends IntentService {
private int result = Activity.RESULT_CANCELED;
private String imageUrl = "http://onman.ir/colorfinder/sample.jpg";
private String folderName = "musafir downloads";
private String fileName = "sampleImage.png";
NotificationCompat.Builder notification;
Notification noti;
NotificationManager manager;
private static final int NOTIFICATION_ID = 1;
private File downloadedFile;
public DownloadService() {
super("Download Service");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.e("<><>", "Service started");
createNotification("Download in progress");
downloadedFile = new File(Environment.getExternalStorageDirectory()+"/"+folderName, fileName);
if(downloadedFile.exists())
{
downloadedFile.delete();
}
InputStream is = null;
FileOutputStream fos = null;
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
try{
Log.e("<><>", "Inside try");
URL url = new URL(imageUrl);
/* HttpURLConnection conn = (HttpURLConnection) url.openConnection();
try{
conn.setRequestMethod("GET");
conn.connect();
if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){
is = conn.getInputStream();
}
}catch (Exception ex)
{
}*/
URLConnection urlConnection = url.openConnection();
int fileSize = urlConnection.getContentLength();
Log.e("size", fileSize+"");
is = urlConnection.getInputStream();
fos = new FileOutputStream(downloadedFile);
byte[] data = new byte[fileSize];
int incr = fileSize/100;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
int count = -1;
int progress = 0;
while((count = is.read(data, 0, incr))!=-1)
{
progress += count;
int per = (progress * 100/fileSize);
outStream.write(data, 0, count);
publishProgress(per);
}
bitmap = BitmapFactory.decodeByteArray(outStream.toByteArray(), 0, data.length);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
//successfully finished
result = Activity.RESULT_OK;
}catch (Exception ex)
{
}finally {
if(is!=null)
{
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos!=null)
{
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
publishResult("Download completed");
Log.e("<><>", "Download finished");
}
private void publishProgress(final int per) {
new Thread(new Runnable() {
@Override
public void run() {
notification.setContentText("Download in progress");
notification.setProgress(100, per, false);
noti = notification.build();
noti.flags |= Notification.FLAG_AUTO_CANCEL;
noti.flags |= Notification.FLAG_NO_CLEAR;
manager.notify(NOTIFICATION_ID, noti);
// startForeground(NOTIFICATION_ID, noti);
}
});
}
private void publishResult(String status) {
if(result==Activity.RESULT_OK)
{
//notification.setProgress(0, 0, true);
/*notification.setContentText("Download completed");*/
/*createNotification("Download completed");*/
notification.setContentText(status);
notification.setProgress(0, 0, false);
noti = notification.build();
noti.flags |= Notification.FLAG_AUTO_CANCEL;
noti.flags |= Notification.FLAG_NO_CLEAR;
manager.notify(NOTIFICATION_ID, noti);
// startForeground(NOTIFICATION_ID, noti);
}
}
private void createNotification(String status) {
Intent notifyintent = new Intent(DownloadService.this, NotificationProcessorActivity.class);
if(downloadedFile!=null) {
notifyintent.setAction(Intent.ACTION_VIEW);
notifyintent.setDataAndType(Uri.fromFile(downloadedFile), "image/*");
}
PendingIntent pIntent = PendingIntent.getActivity(this, 1000, notifyintent, 0);
//Build notification
notification = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setContentTitle("Image Download")
.setContentText(status).setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pIntent)
.setProgress(0,0, true)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
noti = notification.build();
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
noti.flags |= Notification.FLAG_NO_CLEAR;
noti.flags |= Notification.PRIORITY_HIGH;
startForeground(NOTIFICATION_ID, noti);
}
}
答案 0 :(得分:0)
通知正在被驳回,因为在publishResult()
功能中,您的进度为setProgress(0, 0, false)
尝试做:
setProgress(100, 100, false);