文章

顯示從 1月, 2024 起發佈的文章

Download File into app specific storage with Retrofit

圖片
  In the previous post , DownloadManager is used to download files into the device’s public directory. If the file contains sensitive data, we hope the file can be saved into the app-specific storage rather than public directory. Therefore, we need another method to download. Retrofit is a type-safe  HTTP client which help us transfer information and data over a network. Dependency Add below dependency into module build.gradle file. implementation( "com.squareup.retrofit2:retrofit:2.9.0" ) implementation( "com.squareup.retrofit2:converter-gson:2.9.0" ) We add codes on the same module of DownloadManager. A button is added next to the DownLoadManager Button. When it is clicked, it will start to download the file using Retrofit. Implementation We first create a interface so that Retrofit can translate the java/Kotlin function to HTTP API.  The notation @Streaming is used for downloading large file so that it would not write too large data into memory at once. The do...

Android Download file to public storage

圖片
  To implement the function of “download file” from the internet quickly, we can use DownloadManager . After we have input the appropriate parameter, the system will automatically assign the “download“ to a thread running in background and execute it. That means we do not create a thread and create the DownloadManager in the thread. Notification could be shown during the download. However, there are limitations with DownloadManager . The location to save the download file is limited to the public directory: Downloads, Pictures, Movies, etc Our app cannot have information about the download progress. That means DownloadManger doesn’t report how many bytes out of the total file size have been downloaded until the download is completed and it will broadcast ACTION_DOWNLOAD_COMPLETE. We may need to create a thread to keep on monitoring the thread. There is no method of DownloadManager to find out the original filename stored on the web.  If the download link does not contain the...

Android: Check network state and WiFi SSID

Some app may need to download large file and mobile data usually is charged. To avoid inducing extra fee to the user, it should check the network type before start to download. This post shows how to check the type (mobile or WiFi) of network connection. If WiFi network is used, we can also get the local network name (SSID). Add Persmisions First, we add below permission in the AndroidManifest.xml file <uses-permission android:name= "android.permission.INTERNET" /> <uses-permission android:name= "android.permission.ACCESS_NETWORK_STATE" /> Check the instant Network status We get an instance of class ConnectivityManager from system service with Context.CONNECTIVITY_SERVICE. It provide the instant network state. We call ConnectivityManager.getActiveNetwork() to have the network of the system currently use. Then we check the network type from the object NetworkCapabilites . fun updateActiveNetworkFlags (){ val connectivityManager ...