I have to create an application that downloads files from a server using IntentService, I have created the Downloading Service and I pass it down to the receiver but I have no idea where to go from there.The result should be an Array String that will populate a listView through an adapter. But I don't get how I could pass the result from the receiver to the MainActivity where my adapter is.
public class MainActivity extends AppCompatActivity {
ArrayList<PlaylistItem> arrayOfUsers = new ArrayList<PlaylistItem>();
// Create the adapter to convert the array to views
PlaylistAdapter adapter = new PlaylistAdapter(this, arrayOfUsers);
// Attach the adapter to a ListView
ListView listView = (ListView) findViewById(R.id.listView);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent downloadIntent = new Intent(this, DownloadService.class);
downloadIntent.putExtra("url", "url to be called" );
downloadIntent.putExtra("type","playlist");
downloadIntent.putExtra("receiver", new DownloadReceiver(new Handler()));
startService(downloadIntent);
listView.setAdapter(adapter);
}
}
The downloadService
public class DownloadService extends IntentService {
public static final int UPDATE_PROGRESS = 1000;
public static final int PLAYLIST_READY = 2000;
private ResultReceiver receiver;
public DownloadService() {
super("DownloadService");
}
@Override
protected void onHandleIntent(Intent intent) {
String urlToDownload = intent.getStringExtra("url");
receiver = (ResultReceiver) intent.getParcelableExtra("receiver");
String type= intent.getStringExtra("type");
// se apeleaza ori downloadPlaylist ori downloadMusic
// in functie de ce url am primit in intent
if(type=="playlist")
{
downloadPlaylist(urlToDownload);
}
}
private void downloadPlaylist(String urlToDownload){
String str=null;
try {
URL url = new URL(urlToDownload);
URLConnection connection = url.openConnection();
connection.connect();
// this will be useful so that you can show a typical 0-100% progress bar
int fileLength = connection.getContentLength();
File musicDirectory = new
File(Environment.getExternalStorageDirectory(),"music");
if (!musicDirectory.exists())
musicDirectory.mkdirs();
// download the file
InputStream input = new BufferedInputStream(connection.getInputStream());
OutputStream output = new FileOutputStream(musicDirectory+urlToDownload);
str=input.toString();
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
Bundle resultData = new Bundle();
resultData.putInt("progress" ,(int) (total * 100 / fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
}
Bundle resultData = new Bundle();
resultData.putInt("progress" ,100);
receiver.send(UPDATE_PROGRESS, resultData);
}
public class DownloadReceiver extends ResultReceiver {
public DownloadReceiver(Handler handler) {
super(handler);
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
if (resultCode == UPDATE_PROGRESS) {
//send data to Main ACtivity
// update progress bar
}
}
}
I don't get how I could call the array from receiver in MainActivity or vice-versa to populate it. If you could give me an example. Thank you !