java—对象的值如何从接收方传递到mainactivity(或调用类)?

kpbpu008  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(284)

我必须使用intentservice创建一个从服务器下载文件的应用程序,我已经创建了下载服务并将其传递给接收者,但我不知道从哪里开始。结果应该是一个数组字符串,它将通过适配器填充listview。但我不知道如何将结果从接收器传递到适配器所在的主活动。

  1. public class MainActivity extends AppCompatActivity {
  2. ArrayList<PlaylistItem> arrayOfUsers = new ArrayList<PlaylistItem>();
  3. // Create the adapter to convert the array to views
  4. PlaylistAdapter adapter = new PlaylistAdapter(this, arrayOfUsers);
  5. // Attach the adapter to a ListView
  6. ListView listView = (ListView) findViewById(R.id.listView);
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. Intent downloadIntent = new Intent(this, DownloadService.class);
  12. downloadIntent.putExtra("url", "url to be called" );
  13. downloadIntent.putExtra("type","playlist");
  14. downloadIntent.putExtra("receiver", new DownloadReceiver(new Handler()));
  15. startService(downloadIntent);
  16. listView.setAdapter(adapter);
  17. }
  18. }

下载服务

  1. public class DownloadService extends IntentService {
  2. public static final int UPDATE_PROGRESS = 1000;
  3. public static final int PLAYLIST_READY = 2000;
  4. private ResultReceiver receiver;
  5. public DownloadService() {
  6. super("DownloadService");
  7. }
  8. @Override
  9. protected void onHandleIntent(Intent intent) {
  10. String urlToDownload = intent.getStringExtra("url");
  11. receiver = (ResultReceiver) intent.getParcelableExtra("receiver");
  12. String type= intent.getStringExtra("type");
  13. // se apeleaza ori downloadPlaylist ori downloadMusic
  14. // in functie de ce url am primit in intent
  15. if(type=="playlist")
  16. {
  17. downloadPlaylist(urlToDownload);
  18. }
  19. }
  20. private void downloadPlaylist(String urlToDownload){
  21. String str=null;
  22. try {
  23. URL url = new URL(urlToDownload);
  24. URLConnection connection = url.openConnection();
  25. connection.connect();
  26. // this will be useful so that you can show a typical 0-100% progress bar
  27. int fileLength = connection.getContentLength();
  28. File musicDirectory = new
  29. File(Environment.getExternalStorageDirectory(),"music");
  30. if (!musicDirectory.exists())
  31. musicDirectory.mkdirs();
  32. // download the file
  33. InputStream input = new BufferedInputStream(connection.getInputStream());
  34. OutputStream output = new FileOutputStream(musicDirectory+urlToDownload);
  35. str=input.toString();
  36. byte data[] = new byte[1024];
  37. long total = 0;
  38. int count;
  39. while ((count = input.read(data)) != -1) {
  40. total += count;
  41. // publishing the progress....
  42. Bundle resultData = new Bundle();
  43. resultData.putInt("progress" ,(int) (total * 100 / fileLength));
  44. output.write(data, 0, count);
  45. }
  46. output.flush();
  47. output.close();
  48. input.close();
  49. } catch (IOException e) {
  50. e.printStackTrace();
  51. }
  52. Bundle resultData = new Bundle();
  53. resultData.putInt("progress" ,100);
  54. receiver.send(UPDATE_PROGRESS, resultData);
  55. }
  1. public class DownloadReceiver extends ResultReceiver {
  2. public DownloadReceiver(Handler handler) {
  3. super(handler);
  4. }
  5. @Override
  6. protected void onReceiveResult(int resultCode, Bundle resultData) {
  7. super.onReceiveResult(resultCode, resultData);
  8. if (resultCode == UPDATE_PROGRESS) {
  9. //send data to Main ACtivity
  10. // update progress bar
  11. }
  12. }
  13. }

我不知道如何从mainactivity中的接收器调用数组,或者反过来调用数组来填充它。如果你能给我举个例子。谢谢您!

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题