Java 方法setProgress必须从UI线程调用吗?
我正在尝试通过AsyncTask设置进度条。但是我有一个小问题 我的代码在这里Java 方法setProgress必须从UI线程调用吗?,java,android,multithreading,android-asynctask,Java,Android,Multithreading,Android Asynctask,我正在尝试通过AsyncTask设置进度条。但是我有一个小问题 我的代码在这里 public class DownloadFileFromURL extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { super.onPreExecute(); mBuilder.setProgress(100, 0, fal
public class DownloadFileFromURL extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mBuilder.setProgress(100, 0, false);
mNotifyManager.notify(id, mBuilder.build());
}
@Override
protected String doInBackground(String... f_url) {
try {
//CreateDownloadTransactionInPanel(1);
long total = 0;
URL url = new URL(f_url[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
if(file.exists()){
//6829809
deneme = file.length();
connection.setRequestProperty("Range", "bytes="+(file.length())+"-"); // indirmemiÅŸse atamaz
}
connection.connect();
RandomAccessFile output = new RandomAccessFile(file,"rw");
BufferedInputStream input = new BufferedInputStream(connection.getInputStream());
if (file.length() > 0) {
output.seek(deneme);
total=deneme;
}
byte data[] = new byte[4096];
int lastcount = 0;
while ((count = input.read(data,0,4096)) != -1) {
if (isCanceled) {
downloadresult = false;
break;
}
if (intCheck()) {
total += count;
downloadresult = true;
ProgBarCount = (int) ((total * 100) / lenghtOfFile);
if (ProgBarCount > lastcount) {
lastcount = ProgBarCount;
publishProgress(Integer.toString(ProgBarCount));
pBar.setProgress(ProgBarCount);
}
output.write(data, 0, count);
}
else {
downloadresult = false;
break;
}
} //--While Son--
output.close();
input.close();
}
catch (Exception e) {
e.printStackTrace();
exmessage = e.getMessage().toString();
downloadresult = false;
}
return null;
} // doinBackSON
/**
* Updating progress bar
*/
protected void onProgressUpdate(String... progress) {
// setting progress percentage
mBuilder.setProgress(100, Integer.parseInt(progress[0]), false);
mBuilder.setContentText("Şuan İşlemde : %" + Integer.parseInt(progress[0]) + "");
mNotifyManager.notify(id, mBuilder.build());
super.onProgressUpdate(progress);
}
@Override
protected void onPostExecute(String downloadpath) {
if (exmessage != null) {
Toast.makeText(getApplicationContext(),"hata:"+exmessage, Toast.LENGTH_SHORT).show();
}
if (downloadresult == true) {
if (file.exists() && lenghtOfFile == (int) file.length()) {
Toast.makeText(DownloadActivity.this, "DOSYA INDIRILDI VE YUKLEMEYE HAZIR", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "DOSYA YOK YADA EKSIK INDIRILDI", Toast.LENGTH_SHORT).show();
}
CreateDownloadTransactionInPanel(2);
scheduleNotificationWithTickButton(); // Tick Button
RedirectRebootActivity(); // Burda hata olabilir
} //--True Son
else // False Başlangıç
{
mNotifyManager.cancel(2219);
//scheduleNotificationWithResumeButton(); Devam button
}
}
}
public类下载filefromURL扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
mBuilder.setProgress(100,0,false);
mNotifyManager.notify(id,mBuilder.build());
}
@凌驾
受保护的字符串doInBackground(字符串…f_url){
试一试{
//CreateDownloadTransactionPanel(1);
长总计=0;
URL=新URL(f_URL[0]);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
if(file.exists()){
//6829809
deneme=file.length();
connection.setRequestProperty(“Range”、“bytes=”+(file.length())+“-”;//indirmemiŸŸse atamaz
}
connection.connect();
RandomAccessFile输出=新的RandomAccessFile(文件,“rw”);
BufferedInputStream输入=新的BufferedInputStream(connection.getInputStream());
如果(file.length()>0){
输出寻道(deneme);
总数=deneme;
}
字节数据[]=新字节[4096];
int lastcount=0;
而((count=input.read(data,04096))!=-1){
如果(已取消){
downloadresult=false;
打破
}
if(intCheck()){
总数+=计数;
downloadresult=true;
ProgBarCount=(整数)((总计*100)/长度文件);
如果(ProgBarCount>lastcount){
lastcount=ProgBarCount;
publishProgress(Integer.toString(ProgBarCount));
pBar.setProgress(ProgBarCount);
}
输出.写入(数据,0,计数);
}
否则{
downloadresult=false;
打破
}
}儿子--
output.close();
input.close();
}
捕获(例外e){
e、 printStackTrace();
exmessage=e.getMessage().toString();
downloadresult=false;
}
返回null;
}//多恩巴克森
/**
*更新进度条
*/
受保护的void onProgressUpdate(字符串…进度){
//设置进度百分比
mBuilder.setProgress(100,Integer.parseInt(progress[0]),false);
mBuilder.setContentText(“Şuanİşlemde:%”+Integer.parseInt(progress[0])+”;
mNotifyManager.notify(id,mBuilder.build());
super.onProgressUpdate(进度);
}
@凌驾
受保护的void onPostExecute(字符串下载路径){
if(exmessage!=null){
Toast.makeText(getApplicationContext(),“hata:+exmessage,Toast.LENGTH_SHORT).show();
}
如果(downloadresult==true){
if(file.exists()&&lenghtOfFile==(int)file.length()){
Toast.makeText(DownloadActivity.this,“DOSYA indiridi VE yukleye HAZIR”,Toast.LENGTH_SHORT.show();
}否则{
Toast.makeText(getApplicationContext(),“DOSYA YOK YADA EKSIK indirldi”,Toast.LENGTH_SHORT.show();
}
CreateDownloadTransactionPanel(2);
scheduleNotificationWithTickButton();//勾选按钮
RedirectRebootActivity();//Burda hata olabilir
}真正的儿子
else//False BaŸŸlangıç
{
mNotifyManager.cancel(2219);
//scheduleNotificationWithResumeButton();Devam按钮
}
}
}
Android studio说:必须从UI线程调用该方法setProgress
此行pBar.setProgress(ProgBarCount)代码>
有可能吗,你知道这件事吗
谢谢 您调用PublishProgress
这将在AsyncTask中激发对onProgressUpdate方法的调用
@Override
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
使用此方法执行UI操作
@Overide
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
您可以使用我已被接受的答案的副本。我猜你也投了我的票?真的很抱歉,我一次打开了多个问题,我正在回答其他一些问题,显然页面没有刷新,因此看不到你的答案。而且,没有人故意发布相同的答案,所以要成熟点,不要哭,不要贬低别人的答案,没有人故意这么做。我没有贬低任何答案,也没有哭泣。我以为是你,因为在你的答案公布的准确时间,投了反对票。顺便说一句,页面会自动更新,以便您可以查看是否已经发布了答案。如果您未立即回答问题而将问题保留在打开状态,您可能希望在将来刷新页面以避免添加噪音。你回答的最后一个问题是在这一个问题之前45分钟。再次请看《哭单票》,请超越它,而且,我认为你是一个程序员,要分析一些问题,你需要在IDE中设置代码,并尝试测试相同的问题,然后它就可以解决了,做同样的事情,y花了更多的时间,因为问题没有解决,目前还回答了一个问题,我可能需要生成一个完整的布局,很抱歉,但创建布局至少需要5分钟或10分钟。另外,为了清楚起见,我没有投反对票,我可能现在也投反对票,只是为了更清楚一点,页面会自动刷新,但它不会显示答案,它只是显示一个带有标记“1答案”的水平分隔符,不会显示整个答案,但我也不会弹出,可能是我的浏览器出了一些问题,这是我的错,我很抱歉,但这是无意的。而且,这样的事情也会发生,甚至我的很多答案都与人们在我之后给出的答案相同,t