如何编写一个自定义侦听器,检查下载进程是否已经结束? [英] How can I code a custom listener that checks if download process has ended?
问题描述
这是代码:
活动:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//初始化首选项
settings = PreferenceManager.getDefaultSharedPreferences(this);
//启动数据库
db.open();
db.close();
/ **检查更新** /
checkForDBUpdates(settings.getInt(db_version,0));
}
private void checkForDBUpdates(int Version)
{
//检查Internet连接是否可用
boolean online = isOnline();
if(online)
{
final UpdateNDGS update = new UpdateNDGS(this,Version);
boolean update_status = update.check();
if(update_status)
{
System.out.println(READY TO UPDATE!);
命令updateCommand = new Command()
{
public void execute()
{
update.getUpdate();
update.database();
//下载完成,显示列表
startNormasList();
}
};
AlertDialog updateDialog = createUpdateDialog(this,updateCommand);
updateDialog.show();
}
else
{
//没有下载,显示列表
startNormasList();
}
}
else
{
// offline,show list
startNormasList();
}
}
一切都在工作,但如果我想添加未来的功能性可能会变得混乱。
所以我的问题是:
我该如何改进这个代码?
下载过程完成或从未发生时,添加一个触发事件show list的侦听器不会更好吗?
我该怎么做这样的事情?我已经读了很多,但找不到任何有用的东西。
有听众是增加可扩展性的好方法你的代码以及解除事件和处理程序。以下是您需要执行的步骤:
- 决定在事件的情况下,监听器应实现的接口是触发。这包括在触发显示列表事件的情况下需要传回侦听器的参数。有些东西(我不知道你需要的参数,所以这是非常笼统的结构)
public interface ShowListEventLisener {
public void onShowList(Object ... params);
}
- 在类中,会发生事件,添加一个方法来注册/添加事件的侦听器。请注意,这可以是侦听器或仅一个。
public void setOnShowListListener(ShowListEventLisener showListListener) {
this.showListListener = showListListener;
}
这将允许您增加灵活性,以防您需要不同的行为对于显示列表事件
- 现在您只需要在适当的时间触发该事件。您可以从下载的代码中实现类似于以下代码:
if(isDownloadFinished){
//使用需要传回的参数调用侦听器
this.showListListener.onShowList(paramsThatYouNeed);
}
请注意,由于您正在不同的线程中执行下载,您可能需要使用触发事件之前的Handler / AsyncTask。上面的例子是一般化的,但应该足以让您遵循建立自己的自定义侦听器。
I'm developing an app that downloads some information from internet. At the beginning of the app, a dialog appears and asks the user if he wishes to download that information. If he does, it starts the downloading process (in a different thread).
This is the code:
Activity:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Initialize preferences
settings = PreferenceManager.getDefaultSharedPreferences(this);
// Start Database
db.open();
db.close();
/**Check for updates**/
checkForDBUpdates(settings.getInt("db_version", 0));
}
private void checkForDBUpdates(int Version)
{
// Check if Internet connection is available
boolean online = isOnline();
if (online)
{
final UpdateNDGS update = new UpdateNDGS(this, Version);
boolean update_status = update.check();
if (update_status)
{
System.out.println("READY TO UPDATE!");
Command updateCommand = new Command()
{
public void execute()
{
update.getUpdate();
update.database();
//Download Complete, show list
startNormasList();
}
};
AlertDialog updateDialog = createUpdateDialog(this, updateCommand);
updateDialog.show();
}
else
{
//Nothing to download, show list
startNormasList();
}
}
else
{
//offline, show list
startNormasList();
}
}
Everything is actually working, but if I wish to add functionalitites in the future it can get messy.
So my question is: How can I improve this code?
Wouldn't be better to add a listener that fires the event "show list" when download process is complete or never happened?
How can I do such thing? I've been reading a lot, but couldn't find anything helpfull.
Having listeners are a good way to add extensibility to your code as well decoupling the events and the handlers. Here are the steps that you need to do to achieve that:
- Decide the interface that the listener should implement in the case of the event is triggered. This includes the parameter that you need to pass back to the listener in the case that "show list" event is triggered. Something like (I don't know the parameters you need, so this is very generalized structure)
public interface ShowListEventLisener {
public void onShowList(Object... params);
}
- In the class where the event would occur, add a method to register/add a listener for the event. Note that this could be set of listeners or just one. Following code illustrate adding one listener only
public void setOnShowListListener(ShowListEventLisener showListListener) {
this.showListListener = showListListener;
}
This would allow you add flexibility in case you need different behavior for "show list" event
- Now you just need to trigger the event at appropriate time. You could achieve this similar to the following code from the code that does the download
if( isDownloadFinished) {
// call the listeners with the parameters that you need to pass back
this.showListListener.onShowList(paramsThatYouNeed);
}
Note that since you are executing download in different Thread, you might want to use Handler/AsyncTask before triggering the event. The example above is generalized, but should be enough for you to follow to build your own custom listener.
这篇关于如何编写一个自定义侦听器,检查下载进程是否已经结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!