2017-07-19 107 views
-2

我正在開發像playstore這樣的應用程序,用戶可以在其中下載任何應用程序。我在我的應用程序中有許多應用程序,我通過wp api v2從我的網站獲得了應用程序。當我們點擊任何可用的應用程序細節打開,它有一個下載鏈接。當我們點擊鏈接時,它會進入瀏覽器,但我想要的是當我們點擊任何應用程序下載鏈接下載應該在我的應用程序與進度條開始。我還沒有找到任何適當的解決方案,但在堆棧或任何地方。 Here is the screenshot attached for better understanding. arrow is pointing to the downloading link.點擊網頁鏈接下載應用程序內的文件。

+0

文件下載只是像其他任何請求一樣。如果你有一個已經調用API的應用程序,你當然可以下載文件。 https://futurestud.io/tutorials/retrofit-2-how-to-download-files-from-server – vlatkozelka

+0

兄弟@vlatkozelka我看着它。他正在手動將網址放入活動中。但我有我的應用程序中的很多鏈接,我必須通過單擊鏈接下載 –

+0

這將是一個完全不同的問題,關於如何在應用程序中傳遞數據。 – vlatkozelka

回答

0

您可以使用intent服務下載應用程序。

下面是代碼:

public class DownloadService extends IntentService { 
File cacheDir; 

public DownloadService() { 
    super("DownloadService"); 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
    String tmpLocation = 
      Environment.getExternalStorageDirectory().getPath(); 
    cacheDir = new File(tmpLocation); 
    if (!cacheDir.exists()) { 
     cacheDir.mkdirs(); 
    } 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    String remoteUrl = intent.getExtras().getString("url"); 
    String location; 
    String filename = 
      remoteUrl.substring(
        remoteUrl.lastIndexOf(File.separator) + 1); 
    File tmp = new File(cacheDir.getPath() 
      + File.separator + filename); 
    if (tmp.exists()) { 
     location = tmp.getAbsolutePath(); 

     stopSelf(); 
     return; 
    } 
    try { 
     URL url = new URL(remoteUrl); 
     HttpURLConnection httpCon = 
       (HttpURLConnection) url.openConnection(); 
     if (httpCon.getResponseCode() != 200) 
      throw new Exception("Failed to connect"); 
     InputStream is = httpCon.getInputStream(); 
     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     byte[] buf = new byte[1024]; 
     int n = 0; 
     while (-1 != (n = is.read(buf))) { 
      out.write(buf, 0, n); 
     } 
     out.close(); 
     is.close(); 
     byte[] response = out.toByteArray(); 
     FileOutputStream fos = new FileOutputStream(tmp); 
     fos.write(response); 
     fos.flush(); 
     fos.close(); 
     is.close(); 
     location = tmp.getAbsolutePath(); 
    } catch (Exception e) { 
     Log.e("Service", "Failed!", e); 
    } 
} 
} 

運行與網址的意圖

+0

您現在可以在代碼 – kanduken

+0

中相應地添加進度條Thanku @kanduken讓我試試這個。 –

+0

什麼是writeStream呢? –

0

通過嘗試此代碼這個服務,你可以把這個鏈接(TextView的)的點擊



    private static void downloadFile(String url, File outputFile) { 
     try { 
      URL u = new URL(url); 
      URLConnection conn = u.openConnection(); 
      int contentLength = conn.getContentLength(); 

      DataInputStream stream = new DataInputStream(u.openStream()); 

      byte[] buffer = new byte[contentLength]; 
      stream.readFully(buffer); 
      stream.close(); 

      DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile)); 
      fos.write(buffer); 
      fos.flush(); 
      fos.close(); 
     } catch(FileNotFoundException e) { 
      return; // swallow a 404 
     } catch (IOException e) { 
      return; // swallow a 404 
     } 
    } 

+0

也http://www.coderzheaven.com/2012/04/29/download-file-android-device-remote-server-custom-progressbar-showing-progress/檢查此 –

+0

謝謝@Sonal Bharwani讓我試試這個 –

相關問題