2016-07-22 3 views
0

イム背景に近いですが、問題は、私は、バックグラウンドで私のアプリを閉じたときに、それはアプリがjsonObject 使用して画像や音声をダウンロードしようと

code 
    public class downloadData extends AsyncTask<JSONObject, Void, ArrayList<modelCBlips>> { 
Context context; 
String cId; 
Utils utils; 
private DownloadManager mgr = null; 

public downloadData(Context context, String cId) { 
    this.context = context; 
    this.cId = cId; 
    utils = new Utils(context); 
    mgr = (DownloadManager) context.getSystemService(context.DOWNLOAD_SERVICE); 
    context.registerReceiver(onComplete, 
      new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); 

} 


@Override 
protected ArrayList<modelCBlips> doInBackground(JSONObject... params) { 
    ArrayList<modelCBlips> list = new ArrayList<>(); 
    try { 
     JSONArray blipArray = params[0].getJSONArray("Blips"); 
     for (int i = 0; i < blipArray.length(); i++) { 
      modelCBlips mcblips = new modelCBlips(); 

      JSONObject jObjBlip = blipArray.getJSONObject(i); 

      String imagePath = jObjBlip.getString("imagePath"); 
      String audioPath = jObjBlip.getString("audioPath"); 



      mcblips.setImagePath(SaveFile(imagePath)); 
      mcblips.setAudioPath(SaveFile(audioPath)); 
      list.add(mcblips); 
      if (isCancelled()) 
       System.out.println("istrue"); 
      else 
       System.out.println("not"); 

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

    return list; 
} 


@Override 
protected void onPreExecute() { 
    utils.editor.putString("status" + cId, "Downloading...").commit(); 
} 

@Override 
protected void onPostExecute(ArrayList<modelCBlips> list) { 
    for (int i = 0; i < list.size(); i++) { 
     modelCBlips mcb = list.get(i); 
     utils.con.insertCBlips(mcb.getImagePath(), mcb.getAudioPath()); 
    } 
    utils.editor.putString("status" + cId, "Downloaded").commit(); 
    utils.editor.putBoolean("download" + cId, true).commit(); 

} 

BroadcastReceiver onComplete = new BroadcastReceiver() { 
    public void onReceive(Context ctxt, Intent intent) { 
    } 
}; 

public String SaveFile(String path) { 
    String startdownloadurl = null; 
    try { 
     ContextWrapper cw = new ContextWrapper(context); 
     File directory = cw.getDir("channel" + cId, Context.MODE_PRIVATE); 
     if (!directory.exists()) { 
      directory.mkdir(); 
     } 
     DownloadManager mgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); 

     /* Uri downloadUri = Uri.parse(path); 
     DownloadManager.Request request = new DownloadManager.Request(
       downloadUri); 
     String imgnm = path.substring(path.lastIndexOf("/") + 1); 
     startdownloadurl = directory + "/"; 
     System.out.println(" directory " + startdownloadurl); 
     request.setAllowedNetworkTypes(
       DownloadManager.Request.NETWORK_WIFI 
         | DownloadManager.Request.NETWORK_MOBILE) 
       .setAllowedOverRoaming(false).setTitle("Demo") 
       .setDescription("Something useful. No, really.") 
       .setDestinationInExternalPublicDir(startdownloadurl, imgnm); 

     mgr.enqueue(request); 
     startdownloadurl = directory + "/" + imgnm; 
    */ 
     URL url = new URL(path); 

     String imgnm = path.substring(path.lastIndexOf("/") + 1); 
     URLConnection connection = url.openConnection(); 
     connection.connect(); 
     InputStream input = new BufferedInputStream(url.openStream(), 2048); 

     startdownloadurl = directory + "/" + imgnm; 
     FileOutputStream output = new FileOutputStream(startdownloadurl); 
     byte data[] = new byte[2048]; 
     int bytesRead = 0; 
     while ((bytesRead = input.read(data, 0, data.length)) >= 0) { 
      output.write(data, 0, bytesRead); 
     } 
     output.flush(); 
     output.close(); 
     input.close(); 
    } catch (Exception e) { 
     System.out.println("Exception " + e); 
    } 

    return startdownloadurl; 
} 

} 
うまく動作しない場合でも、ダウンロードマネージャを使用して画像や音声をダウンロードする方法

このコードを使用しています画像と音声ファイルがすべて正常にダウンロードされた後、画像とオーディオファイルをダウンロードしようとしました。

答えて

0

アプリが終了してもサービスが実行されます。

関連する問題