Android 是否从IntentService更新progressbar?
我正在尝试更新progressbar,它集成在我的通知栏中,但我似乎无法让它工作。我知道为什么它不起作用,但我不知道如何解决它。代码如下:Android 是否从IntentService更新progressbar?,android,Android,我正在尝试更新progressbar,它集成在我的通知栏中,但我似乎无法让它工作。我知道为什么它不起作用,但我不知道如何解决它。代码如下: public class DownloadService extends IntentService{ public DownloadService() { super("DownloadService"); } @Override public void onCreate() {
public class DownloadService extends IntentService{
public DownloadService() {
super("DownloadService");
}
@Override
public void onCreate() {
super.onCreate();
ctx = getApplicationContext();
root = new File(Environment.getExternalStorageDirectory()+"/folder-videos/");
if(root.exists() && root.isDirectory()) {
}else{
root.mkdir();
}
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, null, 0);
notification = new Notification(R.drawable.icon, "App", System.currentTimeMillis());
contentView = new RemoteViews(getPackageName(), R.layout.progress_layout);
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.contentView = contentView;
notification.contentIntent = contentIntent;
contentView.setProgressBar(R.id.status_progress, 100, 0, false);
contentView.setTextViewText(R.id.status_text,"Downloading...");
}
@Override
protected void onHandleIntent(Intent intent) {
Intent broadcastIntent = new Intent();
int count;
String full_url = URL + intent.getStringExtra(VIDEOS);
try {
URL url = new URL(full_url);
URLConnection conexion = url.openConnection();
conexion.connect();
File file = new File(root.getPath(), intent.getStringExtra(VIDEOS));
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
contentView.setTextViewText(R.id.status_text,"Downloading " + intent.getStringExtra(VIDEOS));
while ((count = input.read(data)) > 0) {
total += count;
notification.contentView.setProgressBar(R.id.status_progress, 100,(int)((total*100)/lenghtOfFile), false);
Log.e("totaltotal","" + (int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
notificationManager.notify(1,notification);
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e("PRINTSTACK","STACK:" + e.getMessage());
e.printStackTrace();
}
}
}
我知道我必须打电话给:notificationManager.notify(1,notification);在while循环中,我尝试过,但它冻结了应用程序并使其崩溃。是否有其他方法通知progressbar更新的通知管理器
谢谢 尝试使用
notificationManager.notify(1,notification)代码>只有几次
例如:
int lastProgressUpdate=0;
while(...)
{
if(progress%5==0 && progress!=lastProgressUpdate)
{
notificationManager.notify(1,notification);
lastProgressUpdate=progress;
}
}
尝试了5次,但仍然冻结了用户界面。尝试了10个相同的,虽然程度较低,但我仍然可以拉下通知栏,但速度会很慢,而且会冻结。是否有某种NotificationListener侦听更新并通知NotificationManager。它像以前一样进行更新,但是,当我把通知栏拉下来时,它会一直冻结…除了在下载文件时使用while循环更新progressbar之外,还有其他方法吗!!这段代码(while循环)位于IntentService内部和onHandleIntent内部,onHandleIntent作为独立于主线程的线程运行,主线程应该可以。