当我点击按钮时,我想显示一个进度对话框(带有进度条或圆圈,这对我来说无动于衷)。实际上我可以展示它,但我不知道如何在完成onclick里面的操作后隐藏它。这是代码:
copy.setOnClickListener(new OnClickListener() {
public void onClick(View v){
ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setProgressStyle(R.style.NewDialog);
progressDialog.setMessage("Loading...");
progressDialog.show();
String datafolder = Environment.getDataDirectory().getAbsolutePath()+File.separator+"app";
File customfolder=new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+File.separator+"BackupApps");
String comando = "cp -r /data/app /sdcard/BackupApps";
Process suProcess = null;
try {
suProcess = Runtime.getRuntime().exec("su");
} catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
try {
os.writeBytes(comando + "\n");
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
os.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
os.writeBytes("exit\n");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
os.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
我该怎么做?
答案 0 :(得分:1)
首先在你的班级中定义这样的内部类
class SaveTask extends AsyncTask<Void, Void, Void> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(getActivity());
progressDialog.setProgressStyle(R.style.NewDialog);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
String datafolder = Environment.getDataDirectory()
.getAbsolutePath() + File.separator + "app";
File customfolder = new File(Environment
.getExternalStorageDirectory().getAbsolutePath().toString()
+ File.separator + "BackupApps");
String comando = "cp -r /data/app /sdcard/BackupApps";
Process suProcess = null;
try {
suProcess = Runtime.getRuntime().exec("su");
} catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
DataOutputStream os = new DataOutputStream(
suProcess.getOutputStream());
try {
os.writeBytes(comando + "\n");
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
os.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
os.writeBytes("exit\n");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
os.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (progressDialog != null) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}
}
在你的onClick()中调用这个.. 这里我们没有将任何数据传递给AsynchTask所以这里我传递null
copy.setOnClickListener(new OnClickListener() {
public void onClick(View v){
SaveTask task = new SaveTask();
task.execute(null, null, null);
}
答案 1 :(得分:1)
在onClick方法上启动AsyncTask,如下所示:
public void onClick(View v){
YourAsyncTask asyncTask = new AsyncTask(context);
asyncTask.execute();
}
在AsyncTask中,在onPreExecute()上创建一个ProgressDialog,在doInBackground()中执行你的东西,并在onPostExecute()上关闭ProgressDialog:
public class YourAsyncTask extends AsyncTask<Void, Void, Void> {
private Context ctx;
private ProgressDialog progressDialog;
public YourAsyncTask(Context ctx) {
this.ctx = ctx;
}
@Override
protected void onPreExecute() {
ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setProgressStyle(R.style.NewDialog);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
String datafolder = Environment.getDataDirectory().getAbsolutePath()+File.separator+"app";
File customfolder=new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+File.separator+"BackupApps");
String comando = "cp -r /data/app /sdcard/BackupApps";
Process suProcess = null;
try {
suProcess = Runtime.getRuntime().exec("su");
} catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
try {
os.writeBytes(comando + "\n");
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
os.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
os.writeBytes("exit\n");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
os.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
protected void onPostExecute(Void res) {
progressDialog.hide();
progressDialog.dismiss();
}
答案 2 :(得分:1)
不要忘记声明进度Dialog全局变量
@Override
public void onClick(View v) {
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
progressDialog = new ProgressDialog(getActivity());
progressDialog.setProgressStyle(R.style.NewDialog);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
String datafolder = Environment.getDataDirectory().getAbsolutePath()+File.separator+"app";
File customfolder=new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+File.separator+"BackupApps");
String comando = "cp -r /data/app /sdcard/BackupApps";
Process suProcess = null;
try {
suProcess = Runtime.getRuntime().exec("su");
} catch (IOException e3) {
// TODO Auto-generated catch block
e3.printStackTrace();
}
DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
try {
os.writeBytes(comando + "\n");
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
try {
os.flush();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
os.writeBytes("exit\n");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
os.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (progressDialog!=null) {
progressDialog.dismiss();
}
}
};
task.execute((Void[])null);
}
答案 3 :(得分:0)
在progressDialog.dismiss();
和所有onClick
的最后一行添加catch
,以便即使引发Exception
它也会消失