我的AsyncTask的progressDialog没有显示。它似乎是正确的,但它不显示对话框。内部的操作完美但似乎忽略了onPreExecute()
和onPostExecute()
方法..
private class copyApk extends AsyncTask<Void, Void, Void> {
int appPosition;
ProgressDialog mProgressDialog = new ProgressDialog(getActivity());
@Override
protected void onPreExecute(Void pre) {
super.onPreExecute();
mProgressDialog.setTitle("Copy apk");
mProgressDialog.setMessage("Copying...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... apks) {
final File customfolder = new File(Environment.getExternalStorageDirectory().toString()+File.separator+"HazyApkBackup");
if(!customfolder.exists()){
customfolder.mkdirs();
}
try
{
vacca = getActivity().getPackageManager().getApplicationInfo(app.getPackageName(), packageManager.GET_META_DATA).sourceDir.toString();
//Toast.makeText(getActivity(), "Boh "+vacca, Toast.LENGTH_SHORT).show();
process = Runtime.getRuntime().exec("cp " + vacca + " " + customfolder);
//Toast.makeText(getActivity(), "Apk copied in "+customfolder, Toast.LENGTH_SHORT).show();
}
catch (PackageManager.NameNotFoundException | IOException e)
{
Toast.makeText(getActivity(), "Sorry, the apk was not copied correctly", Toast.LENGTH_SHORT).show();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if(mProgressDialog.isShowing())
Toast.makeText(getActivity(), "Apk copied in "+customfolder, Toast.LENGTH_SHORT).show();
mProgressDialog.dismiss();
}
}
由于
答案 0 :(得分:1)
您的onPreExecute()
方法未被调用,因为它的签名不正确。正确的方法没有参数。
@Override
protected void onPreExecute()
{
...
答案 1 :(得分:0)
onPreExecute()
的原型:
@Override
protected void onPreExecute() {
}
在UI线程上调用onPreExecute()
和onPostExecute()
方法。最好使用WeakReference
来避免将来出现异常。
在AsyncTask
中,创建一个WeakReference
,如:
private WeakReference<MyActivity> myWeakContext;
然后在onPreExecute()
,
MyActivity activity = this.myWeakContext.get();
ProgressDialog mProgressDialog = new ProgressDialog(activity);