的AsyncTask强制关闭
问题描述:
任何人都知道为什么的AsyncTask是强制关闭时,我尝试解压缩文件的背景,它不使用的AsyncTask的时候,所以我不知道我做错了工作:的AsyncTask强制关闭
public class UnzipFile extends AsyncTask<Void, Integer, String> {
String zipFile = Environment.getExternalStorageDirectory() + "/download/" + mixtapefilename;
String unzipLocation = Environment.getExternalStorageDirectory() + "/download/";
@Override
protected void onPreExecute() {
Toast toast = Toast.makeText(getApplicationContext(), "Unzipping...", Toast.LENGTH_SHORT);
toast.show();
/* Intent intent = new Intent();
final PendingIntent pendingIntent = PendingIntent.getActivity(Download.this, 0, intent, 0);
// configure the notification
notification = new Notification(R.drawable.zipicon, "Unzipping...", System
.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
notification.setLatestEventInfo(getApplicationContext(), "Please Wait", "Unzipping...", pendingIntent);
notificationManager2.notify(1, notification);*/
}
@Override
protected String doInBackground(Void... params) {
Decompress d = new Decompress(zipFile, unzipLocation);
d.unzip();
return "success";}
@Override
protected void onPostExecute(String result) {
Toast toast = Toast.makeText(getApplicationContext(), "Download Complete", Toast.LENGTH_SHORT);
toast.show();
File oldzip = new File(zipFile);
boolean deleted = oldzip.delete();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
/*notificationManager2.cancel(1);*/
}
}
和那么这里是我的,我想既然你正在使用AsynTask你不能用内部doInBackground主UI线程(即显示敬酒消息)()沟通,在后台
public class Decompress {
byte[] buffer = new byte[1024];
int length;
private String _zipFile;
private String _location;
public Decompress(String zipFile, String location) {
_zipFile = zipFile;
_location = location;
File a = new File(_location + foldername);
if(!a.isDirectory()){
a.mkdirs();
}
_dirChecker("");
}
public void unzip() {
try {
FileInputStream fin = new FileInputStream(_zipFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
if(ze.isDirectory()) {
_dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(_location + ze.getName());
while ((length = zin.read(buffer))>0) {
fout.write(buffer, 0, length);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
Toast toast = Toast.makeText(getApplicationContext(),"Download Complete", Toast.LENGTH_LONG);
toast.show();
} catch(Exception e) {
ProgressDialog dialog;
dialog = new ProgressDialog(Download.this);
dialog.setMessage(e.toString());
dialog.show();
}
}
private void _dirChecker(String dir) {
File f = new File(_location + dir);
if(!f.isDirectory()) {
f.mkdirs();
}
}
}
答
运行unzip部分。
与AsynTask在主UI线程通信的唯一方法是使用onPreExcute()
或onPostExcute()
所以只要删除此行:
Toast toast = Toast.makeText(getApplicationContext(),"Download Complete", Toast.LENGTH_LONG);
+0
谢谢,我打了信后就明白了。对不起这个毫无意义的问题。 –
答
对不起,不同意,但有几种方法从另一个线程在UI线程中执行代码: Activity.runOnUiThread(Runnable接口) View.post(Runnable接口) View.postDelayed(Runnable接口,长) 你可以使用其中的一个才能发布,这将是EXE不管你现在使用的线程如何,都可以通过UI线程来创建。但是,你可以直接使用Log.d(String,String)代替(例如)
答
UI线程是非线程安全的。所以在其他线程中访问UI线程非常危险。您必须通过View.post()
方法
将消息发布到UI消息队列中logcat正在吐出什么异常? –