我正在尝试使用SharedPreferences为我的应用注册帐户,但是当我尝试在saveSipAccount
尝试使用SharedPreferences时,它总是返回与我在getSipAccount
保存的值不同的值。它只在我重新启动我的应用程序时才有效。
我是android和SIP的新手所以请帮助我,非常感谢你!
这是我的代码
private static String PREFERENCE_NAME = "voip_demo_pref";
private static String SIP_DOMAIN_KEY = "sip_domain";
private static String SIP_PROXY_KEY = "proxy";
private static String SIP_USER_KEY = "user";
private static String SIP_PASSWORD_KEY = "password";
public void saveSipAccount(Context context, String domain, String proxy, String user, String password) {
SipAccount account = new SipAccount(domain, proxy, user, password);
saveSipAccount(context, account);
}
public void saveSipAccount(Context context, SipAccount account) {
SharedPreferences pref = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor e = pref.edit();
e.putString(SIP_DOMAIN_KEY, account.getDomain());
e.putString(SIP_PROXY_KEY, account.getProxy());
e.putString(SIP_USER_KEY, account.getUser());
e.putString(SIP_PASSWORD_KEY, encryptString(account.getPassword()));
e.apply();
}
public SipAccount getSipAccount(Context context) {
SipAccount account = new SipAccount();
SharedPreferences pref = context.getSharedPreferences(PREFERENCE_NAME, context.MODE_PRIVATE);
account.setDomain(pref.getString(SIP_DOMAIN_KEY, ""));
account.setProxy(pref.getString(SIP_PROXY_KEY, ""));
account.setUser(pref.getString(SIP_USER_KEY, ""));
String password = pref.getString(SIP_PASSWORD_KEY, "");
if (!TextUtils.isEmpty(password)) {
password = decryptString(password);
}
account.setPassword(password);
return account;
}
这是我拨打saveSipAccount
String domain = String.valueOf(sipDomainView.getText());
String proxy = String.valueOf(sipProxyView.getText());
String user = String.valueOf(sipUserView.getText());
String password = String.valueOf(sipPasswordView.getText());
sipManager.saveSipAccount(MyApplication.getInstance().getApplicationContext(), domain, proxy, user, password);
Log.d(TAG, "saved");
try {
service.changeAccount();
}
这是getSipAccount
public boolean changeAccount() {
sipManager = SipManager.newInstance();
SipAccount profile = sipManager.getSipAccount(MyApplication.getInstance().getApplicationContext());
AccountConfig config = sipManager.getAccountConfig(app, profile.getDomain(), profile.getProxy()
, profile.getUser(), profile.getPassword());
try {
MyApp.ep.libRegisterThread(Thread.currentThread().getName());
} catch (Exception e) {
Log.e(TAG, "libRegisterThread error.", e);
}
changeAccountStatus("processing");
try {
account.modify(config);
} catch (Exception e) {
Log.e(TAG, "MyAccount.modify error.", e);
changeAccountStatus("Fail registration");
return false;
}
return true;
}
答案 0 :(得分:0)
你可以试试这个
e.commit
()
并创建一个这样的String变量:
String str1 = pref.getString(SIP_DOMAIN_KEY, null)
并记录str1进行检查。希望这能帮到你
答案 1 :(得分:0)
尝试为sharedPreferences创建一个特殊类。例如:
public class MySharedPreferences {
public static final String MyPREFERENCES = "MyPrefs";
public static final String KEY1 = "key1";
public static final String KEY2 = "key2";
// other keys
private static MySharedPreferences mInstance;
private static Context mCtx;
private MySharedPreferences(Context context) {
mCtx = context;
}
public static synchronized MySharedPreferences getInstance(Context context) {
if (mInstance == null) {
mInstance = new MySharedPreferences(context);
}
return mInstance;
}
//this method will save the sip account
public boolean saveSipAccount(String domain, String proxy, String user, String password) {
SipAccount account = new SipAccount(domain, proxy, user, password);
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(YOUR_KEY, account.getDomain());
//other putString
return editor.commit();
}
public SipAccount getSipAccount() {
SipAccount account = new SipAccount();
SharedPreferences sharedPreferences = mCtx.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
String domain = sharedPreferences.getString(YOUR_KEY, ""));
account.setDomain(domain));
//other code
if (!TextUtils.isEmpty(password)) {
password = decryptString(password);
}
account.setPassword(password);
return account;
}
获取SipAccount
SipAccount acount = MySharedPreferences.getInstance(context).getSipAccount();
删除并重新安装您的应用。
答案 2 :(得分:0)
当我使用Tray代替SharedPreference时,我的应用有效! 非常感谢你的回答和你的善意:)!