正在尝试在AsyncTask中添加通知生成器



有人帮我,请

我想在AsyncTask方法中实现一个通知生成器,用于从服务器下载文件。包含进度条的通知,根据文件长度递增,显示下载百分比和剩余MB数,有暂停和恢复,我尝试了一下,但在后台下载时仍然没有显示通知,这是我的代码:

class DownloadFileFromURL extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}

@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
String folder = "/data/data/" + getPackageName() + "/files/";
File directory = new File(folder);

if (!directory.exists()) {
directory.mkdirs();
}
OutputStream output = new FileOutputStream(folder + getIntent().getStringExtra("title") + ".mp4");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}

protected void onProgressUpdate(String... progress) {
pDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String file_url) {
dismissDialog(progress_bar_type);
String type = "downloads";
Update(type);
View layout = getLayoutInflater().inflate(R.layout.toast_custom, findViewById(R.id.custom_toast_layout_id));
TextView text = layout.findViewById(R.id.text);
text.setText(getIntent().getStringExtra("title") + "  - Downloaded Successfully!");
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}

}

显示Notification的代码在哪里?您使用的是哪个android版本的操作系统?

最新更新