2017-07-19 105 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让我试试这个 –

相关问题