正如 tle 所述,我需要使用 AsyncTask 下载两个文件。当我尝试时,什么都没有下载。如果需要,我可以给一个 logcat。我不确定为什么它不会下载文件。这是我的代码:
package com.cydeon.smirkit;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.TimeoutException;
import com.stericson.RootTools.*;
import com.stericson.RootTools.exceptions.RootDeniedException;
import com.stericson.RootTools.execution.CommandCapture;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Stock extends Activity implements OnClickListener {
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;
private String urlPath = "https://dl.dropbox.com/s/";
private String[] fileNames = {"u-boot_stock.bin","u-boot_stock.md5"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stock);
Button bInstallS = (Button) findViewById(R.id.bInstallS);
Button bSkipS = (Button) findViewById(R.id.bSkipS);
bInstallS.setOnClickListener(this);
bSkipS.setOnClickListener(this);
mProgressDialog = new ProgressDialog(Stock.this);
mProgressDialog.setMessage("Downloading..." );
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
}
public void onClick(View arg0) {
switch(arg0.getId()){
case R.id.bInstallS:
AlertDialog.Builder builder = new AlertDialog.Builder(Stock.this);
builder.setPositiveButton(R.string.continueStock, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
startDownload();
}
});
builder.setNegativeButton(R.string.cancelStock, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Stock.this.finish();
}
});
builder.setMessage(R.string.messageStock);
builder.setTitle(R.string.titleStock);
AlertDialog dialog = builder.create();
dialog.show();
break;
case R.id.bSkipS:
Intent skip = new Intent(Stock.this, Twrp.class);
startActivity(skip);
}
}
private void startDownload() {
if(checkExternalMedia()==true) {
File file = null;
for(int i=0; i<fileNames.length; i++) {
file = new File(Environment.getExternalStorageDirectory()+fileNames[i]);
boolean exists = file.exists();
if(exists){
continue;
}
else {
new DownloadFileAsync().execute(urlPath+"2mtbqt7mk5khqu3/"+fileNames[i],"klw4vb0valwds2p/"+fileNames[i]);
}
file = null;
}
}
else {
}
}
/** Method to check whether external media available and writable. */
private boolean checkExternalMedia(){
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
boolean stat;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// Can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
stat = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// Can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
stat = false;
} else {
// Can't read or write
mExternalStorageAvailable = mExternalStorageWriteable = false;
stat = false;
}
return stat;
}
class DownloadFileAsync extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
}
protected String doInBackground(String... sURL) {
try{
URL url = new URL(sURL[0]);
URLConnection connection = url.openConnection();
connection.connect();
//Shows 0-100% progress bar
int fileLength = connection.getContentLength();
//Download the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/"+fileNames);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
//Publish the Progress
publishProgress((int) (total * 100/fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
}
return null;
}
@Override
protected void onPostExecute(String unused) {
mProgressDialog.dismiss();
Context context = getApplicationContext();
CharSequence text = "Installing. Please Wait";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
RootTools.remount("/system", "rw");
CommandCapture command = new CommandCapture(0, "su");
try {
RootTools.getShell(true).add(command).waitForFinish();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
} catch (RootDeniedException e) {
e.printStackTrace();
}
}
}
}
因此,如果需要,我可以再次提供 logcat。这是我第一次尝试,所以我不确定我在做什么..