Java 正在尝试在AsyncTask中添加通知生成器
有人帮我吗 我想在AsyncTask方法中实现一个通知生成器,用于从服务器下载文件。包含进度条的通知,根据文件长度递增,显示下载百分比和剩余的MB数,暂停和恢复,我尝试过,但在后台下载时仍不显示通知我的代码:Java 正在尝试在AsyncTask中添加通知生成器,java,android,download,notifications,Java,Android,Download,Notifications,有人帮我吗 我想在AsyncTask方法中实现一个通知生成器,用于从服务器下载文件。包含进度条的通知,根据文件长度递增,显示下载百分比和剩余的MB数,暂停和恢复,我尝试过,但在后台下载时仍不显示通知我的代码: class DownloadFileFromURL extends AsyncTask<String, String, String> { @Override protected void onPreExecute() {
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();
}
}
class DownloadFileFromURL扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
显示对话框(进度条类型);
}
@凌驾
受保护的字符串doInBackground(字符串…f_url){
整数计数;
试一试{
URL=新URL(f_URL[0]);
URLConnection conconnection=url.openConnection();
conconnect.connect();
int lenghtOfFile=conconnect.getContentLength();
InputStream输入=新的BufferedInputStream(url.openStream(),8192);
String folder=“/data/data/”+getPackageName()+“/files/”;
文件目录=新文件(文件夹);
如果(!directory.exists()){
mkdirs()目录;
}
OutputStream输出=新文件OutputStream(文件夹+getIntent().getStringExtra(“标题”)+“.mp4”);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
出版进度(“+(int)((总计*100)/长度文档));
输出.写入(数据,0,计数);
}
output.flush();
output.close();
input.close();
}捕获(例外e){
Log.e(“错误:,e.getMessage());
}
返回null;
}
受保护的void onProgressUpdate(字符串…进度){
pDialog.setProgress(Integer.parseInt(progress[0]));
}
@凌驾
受保护的void onPostExecute(字符串文件\u url){
解雇对话框(进度条类型);
String type=“下载”;
更新(类型);
视图布局=GetLayoutFlater()。充气(R.layout.toast_custom,findViewById(R.id.custom_toast_layout_id));
TextView text=layout.findviewbyd(R.id.text);
text.setText(getIntent().getStringExtra(“title”)+“-下载成功!”);
Toast Toast=新Toast(getApplicationContext());
toast.setDuration(toast.LENGTH\u LONG);
toast.setView(布局);
toast.show();
}
}
显示通知的代码在哪里?您使用的是哪个android版本的操作系统?mNotifyManager=(NotificationManager)getSystemService(Context.Notification\u服务);mBuilder=newnotificationcompat.Builder(MovieInfoActivity.this);mBuilder.setContentTitle(“下载”).setContentText(“下载中”).setSmallIcon(R.drawable.applogo);新建下载文件fromURL().execute(getIntent().getStringExtra(“视频url”))代码>我的手机是android Oreo的V9版本,您必须为builder设置NotificationChannel。详情: