2014-03-13 39 views
0

在我用的onpause和onstop和ondestroy下面的代码,但它是无用的,当用户关闭wifi保存不完整的文件,但我想在用户关闭wifi时,取消下载。如果关闭wifi,取消下载[android]

我该怎么办?

我应该在代码中添加什么?

我的代码:

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); 
}} 
+0

所以你想取消下载时关闭wifi? –

+0

创建一个线程,在经过一段时间后检查互联网连接。如果发现没有互联网连接,那么你必须取消你的DownloadFileFromURL AsyncTask。 –

回答

1

Taken from this question...

注册的广播接收器:

IntentFilter intentFilter = new IntentFilter(); 
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); 
registerReceiver(broadcastReceiver, intentFilter); 

和接收:

@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. 
     } 
    }  
} 

此方法将检测的改变我n wifi,如果更改是wifi没有连接,请取消下载。

相关问题