0

Ich versuche SharedPreferences zu verwenden, um mein Konto für meine Apps zu registrieren, aber es gibt immer einen anderen Wert als das, was ich bei saveSipAccount beim ersten Mal gespeichert habe, wenn ich versuche SharedPreferences zu bekommen getSipAccount. Es funktioniert nur, wenn ich meine Apps neu starte. Ich bin Neuling für Android und SIP also bitte helfen Sie mir, vielen Dank!Ich kann keinen echten Wert erhalten, wenn ich SharedPreferences verwende

Dies ist mein Code

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; 
} 

und dies ist der Code, wenn ich 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(); 
     } 

nennen und das ist 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

können Sie diese Tasten mit der Frage 'SIP_DOMAIN_KEY' hinzufügen,' SIP_PROXY_KEY', 'SIP_PROXY_KEY',' SIP_PROXY_KEY', –

+0

"** in ersten Mal, wenn ich versuche, SharedPreferences bei getSipAccount ** zu erhalten" hinzufügen Sie Code, wenn Sie speichere mit ** saveSipAccount ** und wenn du anrufst ** getSipAccount ** –

+0

Bitte poste die ganze Klasse, wo du diese Methoden verwendest. – Hetfieldan24

Antwort

0

Sie können versuchen, diese e.commit()

und eine String-Variable wie folgt Creat:

String str1 = pref.getString(SIP_DOMAIN_KEY, null) 

und str1 melden Sie sich zu überprüfen. Hope this help you

+0

Vielen Dank für Ihre Antwort, aber es gibt nur 'Null' Wert zurück ... –

+0

So Sie nicht Ihren Wert mit sharepreference erfolgreich speichern, müssen Sie überprüfen, bevor Wert erhalten. –

0

Versuchen Sie, eine spezielle Klasse für sharedPreferences zu erstellen. Zum Beispiel:

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; 
} 

Für erhalten eine Sipaccount

SipAccount acount = MySharedPreferences.getInstance(context).getSipAccount(); 

löschen und Ihre Anwendung neu zu installieren.

+0

Vielen Dank, ich werde diese Methode versuchen !! –

+0

Zeichenfolge domain = String.valueOf (sipDomainView.getText()); und so weiter, überprüfe ob dieser Wert Null ist. –

0

Meine Apps funktionierten, als ich Tray anstelle von SharedPreference verwendete! Vielen Dank Kerl für Ihre Antwort und Ihre Freundlichkeit :)!

Verwandte Themen