看来您走在正确的轨道上。您将拥有正在处理所有下载的 DownloadService。您DownloadingQueueActivity
可以绑定到此Service。当它绑定时,您可以获得所有必要的信息来更新活动。
至于推送通知,您可以使用监听器。所以为Activiy创建一个新的接口来实现:
public static interface DownloadListener {
void updateViews(int progress);
}
然后将此功能添加到您的活动中。
public class DownloadingQueueActivity {
public void updateViews(int progress) {
runOnUiThread(new Runnable() {
@Override
public void run() {
//update views
}
});
}
}
然后在绑定后将监听器添加到服务中:
private ServiceConnection listenerStarter = new ServiceConnection() {
public void onServiceConnected(ComponentName name, IBinder service) {
DownloadService ds = ((DownloadService.LocalBinder) service).getService();
ds.addListener(DownloadingQueueActivity.this);
}
public void onServiceDisconnected(ComponentName className) {
}
};
然后在您的 DownloadService 中,当需要更新视图时,只需向已在服务中设置的侦听器发送消息(也不要忘记在服务中创建 addListener 函数以及在 onStop() 中删除侦听器活动):
listener.updateViews(30);