Android 通知中的进度条
我尝试将进度条放入通知中。但问题是系统在几秒钟内不会响应,一旦启动,进度条就会在开始时卡住,请注意,即使进度条被卡住,但文件仍在下载 下面是代码,请指导我如何处理进度条Android 通知中的进度条,android,progress-bar,notificationmanager,Android,Progress Bar,Notificationmanager,我尝试将进度条放入通知中。但问题是系统在几秒钟内不会响应,一旦启动,进度条就会在开始时卡住,请注意,即使进度条被卡住,但文件仍在下载 下面是代码,请指导我如何处理进度条 public class StaffChoice extends Activity { public static final int DIALOG_DOWNLOAD_PROGRESS = 0; ProgressBar progressBar; private int progress = 10;
public class StaffChoice extends Activity {
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
ProgressBar progressBar;
private int progress = 10;
Intent MyI;
PendingIntent MyPI;
NotificationManager MyNM;
Notification notification;
NotificationManager notificationManager;
private void startDownload() {
String url = "http://www.domainurl.com/3d.png"; //this is not a valid url
DownloadFileAsync dfa = new DownloadFileAsync();
dfa.setActivity(StaffChoice.this);
dfa.execute(url);
}
public void onTaskCompleted() {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.staffchoices);
MyI = new Intent(getApplicationContext(), MaxAppsAct.class);
MyPI = PendingIntent.getActivity(getApplicationContext(), 0, MyI, 0);
MyNM = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, StaffChoice.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
notification = new Notification(R.drawable.logo, "Downloading...", System.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.staffchoices);
notification.contentIntent = pendingIntent;
notification.contentView.setImageViewResource(R.id.imgIcon, R.drawable.save);
notification.contentView.setTextViewText(R.id.tvText, "Downloading...");
notification.contentView.setProgressBar(R.id.pbStatus, 100, progress, false);
notificationManager = (NotificationManager) getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
notificationManager.notify(42, notification);
startDownload();
}
public class DownloadFileAsync extends AsyncTask<String, String, String> {
StaffChoice activity;
private boolean completed;
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
File folder = new File(Environment.getExternalStorageDirectory() + "/MaxApps");
boolean success = false;
if (!folder.exists()) {
success = folder.mkdirs();
}
if (!success) {
} else {
}
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/MaxApps/3d.png");
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) {}
return null;
}
protected void onProgressUpdate(Integer... progress) {
notification.contentView.setProgressBar(R.id.pbStatus, 100, progress[0], false);
notificationManager.notify(42, notification);
}
@Override
protected void onPostExecute(String unused) {
completed = true;
notifyActivityTaskCompleted();
}
public void setActivity(StaffChoice activity){
this.activity = activity;
if (completed) {
notifyActivityTaskCompleted();
}
}
private void notifyActivityTaskCompleted() {
if (null != activity) {
activity.onTaskCompleted();
}
}
}
}
public class StaffChoice扩展活动{
公共静态最终整型对话框\u下载\u进度=0;
ProgressBar ProgressBar;
私人投资进展=10;
意图MyI;
悬挂式MyPI;
通知经理MyNM;
通知;
通知经理通知经理;
私有void startDownload(){
字符串url=”http://www.domainurl.com/3d.png“;//这不是有效的url
DownloadFileAsync dfa=新的DownloadFileAsync();
setActivity(StaffChoice.this);
执行(url);
}
公共void onTaskCompleted(){
dismissDialog(对话框下载进度);
}
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.staffchoices);
MyI=新意图(getApplicationContext(),MaxAppsAct.class);
MyPI=pendingent.getActivity(getApplicationContext(),0,MyI,0);
MyNM=(NotificationManager)getSystemService(Context.NOTIFICATION\u服务);
意向意向=新意向(此为StaffChoice.class);
final pendingent pendingent=pendingent.getActivity(getApplicationContext(),0,intent,0);
通知=新通知(R.drawable.logo,“下载…”,System.currentTimeMillis());
notification.flags=notification.flags | notification.FLAG_持续事件;
notification.contentView=新的远程视图(getApplicationContext().getPackageName(),R.layout.staffchoices);
notification.contentIntent=pendingent;
notification.contentView.setImageViewResource(R.id.imgIcon,R.drawable.save);
notification.contentView.setTextViewText(R.id.tvText,“下载…”);
notification.contentView.setProgressBar(R.id.pbStatus,100,progress,false);
notificationManager=(notificationManager)getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_服务);
通知经理。通知(42,通知);
startDownload();
}
公共类DownloadFileAsync扩展异步任务{
员工选择活动;
完成私有布尔运算;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
showDialog(对话框下载进度);
}
@凌驾
受保护的字符串背景(字符串…aurl){
整数计数;
试一试{
URL=新URL(aurl[0]);
URLConnection conexion=url.openConnection();
conexion.connect();
int lenghtOfFile=conexion.getContentLength();
Log.d(“ANDRO_ASYNC”,“文件长度:“+lenghtOfFile”);
File folder=新文件(Environment.getExternalStorageDirectory()+“/MaxApps”);
布尔成功=假;
如果(!folder.exists()){
success=folder.mkdirs();
}
如果(!成功){
}否则{
}
InputStream输入=新的BufferedInputStream(url.openStream());
OutputStream output=新文件OutputStream(“/sdcard/MaxApps/3d.png”);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
出版进度(“+(int)((总计*100)/长度文档));
输出.写入(数据,0,计数);
}
output.flush();
output.close();
input.close();
}捕获(例外e){}
返回null;
}
受保护的void onProgressUpdate(整数…进度){
notification.contentView.setProgressBar(R.id.pbStatus,100,进度[0],false);
通知经理。通知(42,通知);
}
@凌驾
受保护的void onPostExecute(字符串未使用){
完成=正确;
notifyActivityTaskCompleted();
}
公共无效设置活动(StaffChoice活动){
这个。活动=活动;
如果(已完成){
notifyActivityTaskCompleted();
}
}
私有void notifyActivityTaskCompleted(){
如果(空!=活动){
activity.onTaskCompleted();
}
}
}
}
我建议不要在线程中使用AsyncTask 要显示通知,请在AysnTask中添加以下代码:
@Override
protected void onProgressUpdate(Integer... progress) {
notification.contentView.setProgressBar(R.id.pbStatus, 100, progress[0], false);
notificationManager.notify(42, notification);
}
更新:logcat中的错误如下所示(关闭对话框导致错误):
您可以尝试以下方法:
class YourActivity extends Activity {
private void startDownload() {
String url = "http://www.domainurl.com/3d.png";
DownloadFileAsync dfa = new DownloadFileAsync();
dfa.setActivity(YourActivity.this);
dfa.execute(url);
}
public void onTaskCompleted() {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
public void dismissDialog(int id){
//...
}
public void showDialog(int id){
//...
}
public class DownloadFileAsync extends AsyncTask<String, String, String> {
YourActivity activity;
private boolean completed;
@Override
protected void onPreExecute() {
super.onPreExecute();
activity.showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... aurl) {
// nothing change
}
protected void onProgressUpdate(Integer... progress) {
notification.contentView.setProgressBar(R.id.pbStatus, 100, progress[0], false);
notificationManager.notify(42, notification);
}
@Override
protected void onPostExecute(String unused) {
completed = true;
notifyActivityTaskCompleted();
}
}
public void setActivity(YourActivity activity){
this.activity = activity;
if ( completed ) {
notifyActivityTaskCompleted();
}
private void notifyActivityTaskCompleted() {
if (null != activity) {
activity.onTaskCompleted();
}
}
}
class YourActivity扩展活动{
私有void startDownload(){
字符串url=”http://www.domainurl.com/3d.png";
DownloadFileAsync dfa=新的DownloadFileAsync();
dfa.setActivity(YourActivity.this);
执行(url);
}
公共void onTaskCompleted(){
dismissDialog(对话框下载进度);
}
公共无效解除对话框(int id){
//...
}
公共void显示对话框(int-id){
//...
}
公共类DownloadFileAsync扩展异步任务{
你的活动;
完成私有布尔运算;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
activity.showDialog(对话框下载进度);
}
@凌驾
受保护的字符串背景(字符串…aurl){
//没什么变化
}
受保护的void onProgressUpdate(整数…进度){
notification.contentView.setProgressBar(R.id.pbStatus,100,进度[0],false);
通知经理。通知(42,通知);
}
@凌驾
受保护的void onPostExecute(字符串未使用){