gpt4 book ai didi

java - Android Asynctask 和 progressDialog

转载 作者:搜寻专家 更新时间:2023-11-01 08:13:51 25 4
gpt4 key购买 nike

我想做的是让我的应用能够下载我服务器上的 mp3。到目前为止,我已经将 mp3 下载到一个音频文件中,但它非常挑剔,不能被打扰以使其正常工作。话虽如此,我希望弹出一个无法取消的进度对话框,这样用户就不会在后台将文件下载到文件夹时中断进度。阅读后似乎 AsyncTask 将是执行此操作的最佳方法,但我无法让它工作。下面是我的代码中的按钮之一。

公共(public)课音乐扩展 Activity{

    public static int  mProgress = 0;
static String filename;
MediaPlayer buttonclicker;
static Toast msg;
public static int totalSize = 0;
public ProgressDialog dialog;
public static boolean isFinished;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music);
buttonclicker = MediaPlayer.create(this, R.raw.button );
Button boomFullDownload = (Button) findViewById(R.id.boomfull);

boomFullDownload.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonclicker.start();
filename = "boomboom.mp3";
new downloadPumphouseShow().execute(filename);

class downloadPumphouseShow extends AsyncTask<String , Void, Void> {

ProgressDialog dialog;

Toast msg;

protected void onPreExecute (){
dialog = new ProgressDialog(context);
msg = Toast.makeText(context, " File Exist ", Toast.LENGTH_LONG);
msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
dialog.setMessage("Please Wait Loading");
dialog.setCancelable(false);
dialog.show();

}
}
});

protected void onPostExecute(Void result) {
dialog.hide();
dialog.dismiss();
}


protected Void doInBackground(String... params) {

String filename = params[0];

try {

//set the download URL, a url that points to a file on the internet
//this is the file to be downloaded
URL url = new URL("http://lepumphouse.com/media/" + filename );

//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

//set up some things on the connection
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);

//and connect!
urlConnection.connect();

//set the path where we want to save the file
//in this case, going to save it on the root directory of the
//sd card.
File Music = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC + "/Pumphouse/Party Cake");

//create a new file, specifying the path, and the filename
if(Music.exists())
msg.show();
else
Music.mkdirs();
//which we want to save the file as.
File file = new File(Music, filename);

//this will be used to write the downloaded data into the file we created
FileOutputStream fileOutput = new FileOutputStream(file);

//this will be used in reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();

//this is the total size of the file
int totalSize = urlConnection.getContentLength();
//variable to store total downloaded bytes
int mProgress = 0;

//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer

//now, read through the input buffer and write the contents to the file
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
//add the data in the buffer to the file in the file output stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength);
//add up the size so we know how much is downloaded
mProgress += bufferLength;
//this is where you would do something to report the pr0gress, like this maybe


}
//close the output stream when done


// progressDialog.dismiss();
fileOutput.close();

//catch some possible errors...
} catch (MalformedURLException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}

return null;
}

}

因此,如果我删除所有处理 asynctask 的代码,它就可以工作,这对用户非常不友好,但文件确实会下载。当我尝试添加进度对话框和后台任务时,它在我身上退出了。我有一种感觉,它与参数有关。

最佳答案

protected void onPreExecute() {
dialog=ProgressDialog.show(mContext, "", "Fetching book oversight");
msg = Toast.makeText(context, " File Exist ", Toast.LENGTH_LONG).show;
super.onPreExecute();
}



protected void onPostExecute(Void result) {
if(dialog!=null)
{
dialog.dismiss();
}
}

试试这个,另一种显示对话框的方式

关于java - Android Asynctask 和 progressDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6867685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com