嗨,我已经创建了下载过程活动,它在按钮单击时运行。此活动在列表项单击时打开。但现在我想在点击 lisitem 时运行下载过程,点击按钮。
ZipDownloader.java
import java.io.File;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
import com.kabelash.sg.util.DecompressZip;
import com.kabelash.sg.util.DownloadFile;
import com.kabelash.sg.util.ExternalStorage;
import com.kabelash.sg.R;
public class ZipDownloader extends Activity {
protected ProgressDialog mProgressDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.zipdownload );
// Keep the screen (and device) active as long as this app is frontmost.
// This is to avoid going to sleep during the download.
getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON );
}
/**
* Invoked when user presses "Start download" button.
*/
public void startDownload( View v ) {
String url = "http://sample.co.uk/sample.zip";
new DownloadTask().execute( url );
}
/**
* Background task to download and unpack .zip file in background.
*/
private class DownloadTask extends AsyncTask<String,Void,Exception> {
@Override
protected void onPreExecute() {
showProgress();
}
@Override
protected Exception doInBackground(String... params) {
String url = (String) params[0];
try {
downloadAllAssets(url);
} catch ( Exception e ) { return e; }
return null;
}
}
//Progress window
protected void showProgress( ) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setTitle( R.string.progress_title );
mProgressDialog.setMessage( getString(R.string.progress_detail) );
mProgressDialog.setIndeterminate( true );
mProgressDialog.setCancelable( false );
mProgressDialog.show();
}
protected void dismissProgress() {
// You can't be too careful.
if (mProgressDialog != null && mProgressDialog.isShowing() && mProgressDialog.getWindow() != null) {
try {
mProgressDialog.dismiss();
} catch ( IllegalArgumentException ignore ) { ; }
}
mProgressDialog = null;
}
}
在 MainActivity.java
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.update:
Intent intent = new Intent(this, ZipDownloader.class);
startActivity(intent);
break;
}
return true;
}
请不要忽视这个问题。在此先感谢并为我的英语感到抱歉。