文章總結自襲烽博客的xmpp中地址爲:http://blog.csdn.net/shimiso/article/details/11225873
大家寫android程序會發現,不同的activity之間經常需要調用一些公共的資源,這裏的資源不僅包括android自身的,還有我們自己的管理服務類,甚至相互之間傳遞一些參數,這裏我仿照struts2的設計,提煉出一個ActivitySupport類,同時抽取一個接口,讓所有的Activity都集成這個類,因爲有了接口,我們便可以採用回調模式,非常方便的傳遞數據和使用公共的資源,這種好處相信大家使用之後都能有深刻的體會,通過接口回調傳遞參數和相互調用的方式無疑是最優雅的,spring和hibernate源碼中曾經大量使用這種結構。
例如:public interface IActivitySupport {
/**}
*
* 獲取用戶配置.
*
* @param loginConfig
* @author sujingbo
* @update 2013-7-6 上午9:59:49
*/
public LoginConfig getLoginConfig();
public class ActivitySupport extends Activity implements IActivitySupport {
@Override
public LoginConfig getLoginConfig() {
LoginConfig loginConfig = new LoginConfig();String a = preferences.getString(Constant.XMPP_HOST, null);String b = getResources().getString(R.string.xmpp_host);loginConfig.setXmppHost(preferences.getString(Constant.XMPP_HOST,getResources().getString(R.string.xmpp_host)));loginConfig.setXmppPort(preferences.getInt(Constant.XMPP_PORT,getResources().getInteger(R.integer.xmpp_port)));loginConfig.setUsername(preferences.getString(Constant.USERNAME, null));loginConfig.setPassword(preferences.getString(Constant.PASSWORD, null));loginConfig.setXmppServiceName(preferences.getString(Constant.XMPP_SEIVICE_NAME,getResources().getString(R.string.xmpp_service_name)));loginConfig.setAutoLogin(preferences.getBoolean(Constant.IS_AUTOLOGIN,getResources().getBoolean(R.bool.is_autologin)));loginConfig.setNovisible(preferences.getBoolean(Constant.IS_NOVISIBLE,getResources().getBoolean(R.bool.is_novisible)));loginConfig.setRemember(preferences.getBoolean(Constant.IS_REMEMBER,getResources().getBoolean(R.bool.is_remember)));loginConfig.setFirstStart(preferences.getBoolean(Constant.IS_FIRSTSTART, true));return loginConfig;
}
}
public class LoginActivity extends ActivitySupport {
private LoginConfig loginConfig;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);setContentView(R.layout.login);loginConfig = getLoginConfig();
}
}