Reputation: 47
in the following code I used of onpause and onstop and ondestroy but it is useless and when turn off wifi by user save incomplete file but I want when turn off wifi by user,canceled download.
what can i do?
what should I add in the code?
my code:
public class DoaMatn1 extends Activity implements OnClickListener {
// .
// .
// .
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.doamatn);
// .
// .
// .
}
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
File sdcard = Environment.getExternalStorageDirectory();
File audioFile = new File(sdcard.getPath() + "/EBKH/basem-tavasol.mp3");
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btnplaydoa :
// .
// .
// .
case R.id.btndowndoa :
if(!new File(Environment.getExternalStorageDirectory().toString() + "/EBKH/basem-tavasol.mp3").exists())
downloadTask = (DownloadFileFromURL) new DownloadFileFromURL().execute(file_url);
}}
@Override
protected void onStop() {
if(downloadTask!=null){
downloadTask.cancel(true);
}
super.onStop();
}
@Override
protected void onPause() {
if(downloadTask!=null){
downloadTask.cancel(true);
}
super.onPause();
}
@Override
protected void onDestroy() {
if(downloadTask!=null){
downloadTask.cancel(true);
}
super.onDestroy();
}
class DownloadFileFromURL extends AsyncTask<String, String, String> {
@Override
protected void onCancelled() {
File file= new File("/sdcard/EBKH/basem-tavasol.mp3");
file.delete();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
int lenghtOfFile = conection.getContentLength();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
OutputStream output = new FileOutputStream("/sdcard/EBKH/basem-tavasol.mp3");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress) {
pDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String file_url) {
dismissDialog(progress_bar_type);
}}
Upvotes: 0
Views: 250
Reputation: 6709
Register a broadcast receiver:
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
registerReceiver(broadcastReceiver, intentFilter);
and receive:
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if(action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
boolean connected = info.isConnected();
if (!connected) {
// Stop the download.
}
}
}
This method will detect a change in wifi and if the change is that the wifi is not connected, cancel the download.
Upvotes: 1