編輯:關於Android編程
/** * 用戶登錄信息管理模塊 * @author LZC */ public class UserManager { private static UserManager mInst = null; private static Object mLock = new Object(); private boolean mHasLogined = false; private boolean mIsLogining = false; private String mUserId = ""; public String mToken = "";//登錄憑證 public String mNickName = ""; public String mUserIcon = "";//用戶頭像 public String mPhoneNum;//手機號 public String mSex;//性別“0”女,“1”代表男 public long mBirth;//生日 public String mEmail;//郵箱 public String mQQ;//QQ /** * * 監聽 * */ //登錄 public interface IOnLogin{ void onLogining(); void onLogined(); void onLoginedFailed(); } //登出 public interface IOnLoginOut{ void onLoginOut(); } //登錄過期 public interface IOnLoginInvalid{ void onLoginInvalid(); } public interface IOnUserInfoChange{ void onIconChange(String userIcon); void onGoldChange(int goldCount); void onNickNameChange(String nickName); } private List mLoginListeners = new ArrayList(); private List mOnLoginOuts = new ArrayList(); private List mOnUserInfoChanges = new ArrayList(); private List mOnLoginInvalids = new ArrayList(); private UserManager(){ } public static UserManager getInst( ){ if(mInst == null){ synchronized(mLock){ if(mInst == null){ mInst = new UserManager(); } } } return mInst; } //添加登錄監聽 public void addLoginListener(IOnLogin listener){ if(mLoginListeners != null && !mLoginListeners.contains(listener)){ mLoginListeners.add(listener); } } //移除登錄監聽 public void removeLoginListener(IOnLogin listener){ if(listener != null && mLoginListeners.contains(listener)) mLoginListeners.remove(listener); } //添加登出監聽 public void addLoginOutListener(IOnLoginOut onLoginOut){ if(mOnLoginOuts != null && !mOnLoginOuts.contains(onLoginOut)){ mOnLoginOuts.add(onLoginOut); } } //移除登出監聽 public void removeLoginOutListener(IOnLoginOut onLoginOut){ if(onLoginOut != null && mOnLoginOuts.contains(onLoginOut)){ mOnLoginOuts.remove(onLoginOut); } } //添加用戶信息和變化監聽 public void addUserInfoListener(IOnUserInfoChange onUserInfoChange){ if(onUserInfoChange != null && !mOnUserInfoChanges.contains(onUserInfoChange)){ mOnUserInfoChanges.add(onUserInfoChange); } } //移除用戶信息變化監聽 public void removeUserInfoListener(IOnUserInfoChange onUserInfoChange){ if(onUserInfoChange != null && mOnUserInfoChanges.contains(onUserInfoChange)){ mOnUserInfoChanges.remove(onUserInfoChange); } } //添加登錄失效監聽 public void addLoginInvalidListener(IOnLoginInvalid onLoginInvalid){ if(onLoginInvalid != null && !mOnLoginInvalids.contains(onLoginInvalid)){ mOnLoginInvalids.add(onLoginInvalid); } } //移除登錄失效監聽 public void removeLoginInvalidListener(IOnLoginInvalid onLoginInvalid){ if(onLoginInvalid != null && mOnLoginInvalids.contains(onLoginInvalid)){ mOnLoginInvalids.remove(onLoginInvalid); } } /** * * 設置數據 */ //注銷 public void logout(Context context){ HttpClientInst.removeCookie(); PreferenceWrapper preferencesWrapper = new PreferenceWrapper(); mHasLogined = false; mIsLogining = false; mUserGold = 0; mUserId = ""; mNickName = ""; mUserIcon = ""; mToken = ""; mPhoneNum = "";//手機號 mSex = "0";//性別“0”女,“1”代表男 mBirth = 0;//生日 mEmail = "";//郵箱 mQQ = "";//QQ mIsSign = 0; UserManager.getInst().updateUserIcon(mUserIcon); preferencesWrapper.setStringValue(Constants.USER_NAME, ""); preferencesWrapper.setStringValue(Constants.USER_PWD, ""); preferencesWrapper.setStringValue(Constants.USER_LOGIN_TYPE, ""); preferencesWrapper.commit(); if(mOnLoginOuts != null){ for(int idx = 0; idx < mOnLoginOuts.size(); ++ idx){ mOnLoginOuts.get(idx).onLoginOut(); } } } /** * 此方法只有登錄接口能調用,其他地方不能隨便調用 * @param userBean * @param hasLogined */ //登錄後,設置用戶信息 public void setUserInfo(UserInfoBean userBean, boolean hasLogined){ saveInfo(userBean); if(hasLogined){ if(!mHasLogined){ mIsLogining = false; mHasLogined = true; if(mLoginListeners != null){ for(int idx = 0; idx < mLoginListeners.size(); ++ idx){ try { mLoginListeners.get(idx).onLogined(); } catch (Exception e) { e.printStackTrace(); } } } } } } //把用戶信息保存到本地,只有這三個信息保存到本地,為沒登錄時登錄做准備,其他的都要登錄以後才能獲得,所以只能放在全局變量裡面 private void saveInfo( UserInfoBean userBean){ mUserId = userBean.userId; mUserIcon = userBean.userIcon; mToken = userBean.token; mNickName = userBean.nickName; mPhoneNum = userBean.phoneNum;//手機號 mSex = userBean.sex;//性別“0”女,“1”代表男 mBirth = userBean.birth;//生日 mEmail = userBean.email;//郵箱 mQQ = userBean.qq;//QQ mIsSign = userBean.isSign; PreferenceWrapper preferencesWrapper = new PreferenceWrapper(); preferencesWrapper.setStringValue(Constants.USER_NAME, userBean.userName); preferencesWrapper.setStringValue(Constants.USER_PWD,userBean.password); preferencesWrapper.setStringValue(Constants.USER_LOGIN_TYPE, userBean.loginType); preferencesWrapper.commit(); } //設置當前處於登陸中的狀態 public void setLogining(){ mIsLogining = true; if(mLoginListeners != null){ for(int idx = 0; idx < mLoginListeners.size(); ++ idx){ try { mLoginListeners.get(idx).onLogining(); } catch (Exception e) { e.printStackTrace(); } } } } //設置登錄失敗 public void setLoginFailed() { mIsLogining = false; mHasLogined = false; if(mLoginListeners != null){ for(int idx = 0; idx < mLoginListeners.size(); ++ idx){ try { mLoginListeners.get(idx).onLoginedFailed(); } catch (Exception e) { e.printStackTrace(); } } } } public void setLoginOutOfDate(){ mHasLogined = false; } /** * 更改用戶信息後更新用戶數據 */ //修改密碼後調用 public void updateUserPassword(String password,Context context) { PreferenceWrapper preferencesWrapper = new PreferenceWrapper(); preferencesWrapper.setStringValue(Constants.USER_PWD, password); preferencesWrapper.commit(); } //修改頭像後調用 public void updateUserIcon(String userIcon){ mUserIcon = userIcon; for(int i = 0; i < mOnUserInfoChanges.size(); i ++){ mOnUserInfoChanges.get(i).onIconChange(mUserIcon); } } //修改昵稱後調用 public void updateNickName(String nickName){ mNickName = nickName; for(int i = 0; i < mOnUserInfoChanges.size(); i ++){ mOnUserInfoChanges.get(i).onNickNameChange(mNickName); } } /*** * 獲取數據 */ //判斷當前是否是正在登陸中的狀態 public boolean isLogining(){ return mIsLogining; } public boolean isLogined(){ return mHasLogined; } public ProtocolLogin createLoginProtocol(Context context, IProtocolListener listener){ return new ProtocolLogin(context,ProtocolLogin.TYPE_AUTO, getLoginType(), getUserName(), getPassword(), listener); } public String getLoginType() { PreferenceWrapper preferencesWrapper = new PreferenceWrapper(); return preferencesWrapper.getStringValue(Constants.USER_LOGIN_TYPE, ""); } //獲取用戶名 public String getUserName(){ PreferenceWrapper preferencesWrapper = new PreferenceWrapper(); return preferencesWrapper.getStringValue(Constants.USER_NAME, ""); } //獲取密碼 public String getPassword(){ PreferenceWrapper preferencesWrapper = new PreferenceWrapper(); return preferencesWrapper.getStringValue(Constants.USER_PWD, ""); } //是否可以自動登錄 public boolean canAutoLogin(){ PreferenceWrapper preferencesWrapper = new PreferenceWrapper(); String username = preferencesWrapper.getStringValue(Constants.USER_NAME, ""); String password = preferencesWrapper.getStringValue(Constants.USER_PWD, ""); return !(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)); } //用戶id public String getUserId(){ return mUserId; } //用戶登錄令牌 public String getToken(){ return mToken; } //用戶圖標 public String getUserIcon(){ return mUserIcon; } //用戶昵稱 public String getNickName(){ return mNickName; } public String getmPhoneNum() { return mPhoneNum; } public String getmSex() { return mSex; } public long getmBirth() { return mBirth; } public String getmEmail() { return mEmail; } public String getmQQ() { return mQQ; } }
private static UserManager mInst = null; public static UserManager getInst( ){ if(mInst == null){ synchronized(mLock){ if(mInst == null){ mInst = new UserManager(); } } } return mInst; }
public void addUserInfoListener(IOnUserInfoChange onUserInfoChange){ if(onUserInfoChange != null && !mOnUserInfoChanges.contains(onUserInfoChange)){ mOnUserInfoChanges.add(onUserInfoChange); } } 如果頭像發生了修改則調用 public void updateUserIcon(String userIcon){ mUserIcon = userIcon; for(int i = 0; i < mOnUserInfoChanges.size(); i ++){ mOnUserInfoChanges.get(i).onIconChange(mUserIcon); } }
public void removeUserInfoListener(IOnUserInfoChange onUserInfoChange){ if(onUserInfoChange != null && mOnUserInfoChanges.contains(onUserInfoChange)){ mOnUserInfoChanges.remove(onUserInfoChange); } }
今天這篇文章,我們來看看Canvas。Canvas 是畫布,來響應繪畫(Draw)的調用(並將其寫入Btmap)。我們先看看官方文檔對Canvas的描述:The Canv
開發隨筆,小結項目開發中的得與失,要點如下:進展-推薦功能及相關頁面:1.相關控件的使用:TimePicker、RadioGroup、RadioButton、Button
導語 本文主要是圍繞android直播助手的功能做了一些研究,因為之前對Android多媒體相關的內容知之甚少,只有概念,於是查閱了相關資料並做以總結。由於我對音視頻相關
Service概念及用途:Android中的服務,它與Activity不同,它是不能與用戶交互的,不能自己啟動的,運行在後台的程序,如果我們退出應用時,Service進程