Android 在服务中使用AsyncTask更新通知栏中的Progressbar?
我正在尝试使用一个服务下载多个二进制文件,该服务包含一个用于进行后台工作的AsynTask。我能够成功运行该服务,我已经在清单文件中注册了它。我可以下载文件,但是我想在通知栏中显示一个进度条。我正在onPreExecute()方法内创建通知栏,设置progressbar并在onProgressUpdate()方法内通知NotifactionManagerAndroid 在服务中使用AsyncTask更新通知栏中的Progressbar?,android,Android,我正在尝试使用一个服务下载多个二进制文件,该服务包含一个用于进行后台工作的AsynTask。我能够成功运行该服务,我已经在清单文件中注册了它。我可以下载文件,但是我想在通知栏中显示一个进度条。我正在onPreExecute()方法内创建通知栏,设置progressbar并在onProgressUpdate()方法内通知NotifactionManager 私有类下载FileAsync扩展异步任务{ @凌驾 受保护的void onPreExecute(){ super.onPreExecute()
私有类下载FileAsync扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
notificationManager=(notificationManager)getSystemService(Context.NOTIFICATION\u服务);
通知=新通知(R.drawable.icon,“下载…”,System.currentTimeMillis());
contentView=新的远程视图(getPackageName(),R.layout.progress\u布局);
setProgressBar(R.id.status\u progress,10,0,false);
setTextViewText(R.id.status_text,currentFile);
notification.contentView=contentView;
//Toast.makeText(DownloadService.this,“下载…”,Toast.LENGTH\u SHORT.show();
}
@凌驾
受保护的字符串背景(字符串…aurl){
整数计数;
试一试{
//下载代码到这里
}捕获(例外e){}
返回null;
}
受保护的void onProgressUpdate(字符串…进度){
Log.d(“ANDRO_ASYNC”,progress[0]);
notification.contentView.setProgressBar(R.id.status_progress,100,Integer.parseInt(progress[0]),false);
//通知进度条正在进行的更新
notificationManager.notify(通知ID,通知);
}
@凌驾
受保护的void onPostExecute(字符串未使用){
Toast.makeText(DownloadService.this,“下载完成!”,Toast.LENGTH_SHORT.show();
}
}
DownloadFileAsync私有类位于扩展服务的DownloadService类内。我无法显示或更新通知栏
我目前在网上收到一个IllegalArgumentException:
notificationManager.notify(通知ID,通知)
非常感谢你在这件事上的帮助
编辑:NOTIFICATION\u ID的定义如下:private int NOTIFICATION\u ID=R.string.app\u name回答这个问题可能晚了,但我目前正在编写一个具有类似代码的应用程序 NOTIFICATION_ID必须是int类型,因为notify()的第一个参数采用int而不是代码中的字符串 以下是有关notify及其不同签名的更多信息:
)我认为您需要将通知添加到通知管理器。我对此不确定。请尝试让我知道。这是我的代码:
notificationManager = (NotificationManager) getApplicationContext().getSystemService(
getApplicationContext().NOTIFICATION_SERVICE);
notificationManager.notify(10, notification);
// simulate progress
Thread download = new Thread() {
@Override
public void run() {
for (int i = 1; i < 100; i++) {
progress++;
notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false);
// inform the progress bar of updates in progress
notificationManager.notify(id, notification);
try {
Thread.sleep(125);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
download.run();
notificationManager=(notificationManager)getApplicationContext().getSystemService(
getApplicationContext().NOTIFICATION_服务);
通知经理。通知(10,通知);
//模拟进度
线程下载=新线程(){
@凌驾
公开募捐{
对于(int i=1;i<100;i++){
进步++;
notification.contentView.setProgressBar(R.id.status\u progress,100,progress,false);
//通知进度条正在进行的更新
notificationManager.notify(id,通知);
试一试{
睡眠(125);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
};
下载.运行();
notificationManager = (NotificationManager) getApplicationContext().getSystemService(
getApplicationContext().NOTIFICATION_SERVICE);
notificationManager.notify(10, notification);
// simulate progress
Thread download = new Thread() {
@Override
public void run() {
for (int i = 1; i < 100; i++) {
progress++;
notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false);
// inform the progress bar of updates in progress
notificationManager.notify(id, notification);
try {
Thread.sleep(125);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
download.run();