How to use an intent to update an activity?

前端 未结 2 1010
臣服心动
臣服心动 2020-12-28 08:12

I have a service that is downloading a file. When the download is done, I would like to update my \"Downloaded files\" list in my Activity, but only if the

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-28 09:17

    You can create new BroadcastReceiver instance and do something along these lines on your Activity's onResume() method:

    registerReceiver(myReceiver, new IntentFilter(DownloadService.ACTION_FILE_DOWNLOADED));
    

    After that, override myReceiver's onReceive() method to call a function that updates the component you want:

    @Override 
    public void onReceive(Context context, Intent intent) {
    ...
        updateViewWithData(service.getNewFileData());
    ...
    }
    

    On your Activity's onPause() method, just unregister the receiver:

    unregisterReceiver(myReceiver);
    

    I hope that this would help you, feel free to ask if there is something unclear.

提交回复
热议问题