2017-08-22 13 views
0

SharedPreferencesを使って自分のアプリケーションのアカウントを登録しようとしていますが、SharedPreferencesを初めて取得しようとしたときに初めてsaveSipAccountに保存した値getSipAccount。私のアプリを再起動したときだけ動作します。 私はアンドロイドとSIPのための初心者ですので、私を助けてください、ありがとうございます! 私はSharedPreferencesを使用すると本当の価値を得ることができません

この

は私のコード

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

このキーに「SIP_DOMAIN_KEY」、「SIP_PROXY_KEY」、「SIP_PROXY_KEY」、「SIP_PROXY_KEY'、 –

+0

」という**キーを追加することはできますか?初めてgetSipAccount **でSharedPreferencesを取得しようとしました。 ** saveSipAccount **を使用して保存しています。** getSipAccount ** –

+0

を呼び出すときは、これらのメソッドを使用するクラス全体を投稿してください。 – Hetfieldan24

答えて

0

あるこの e.commit()

と、このような文字列変数クリート:

String str1 = pref.getString(SIP_DOMAIN_KEY, null) 

をチェックするSTR1を記録。これがあなたに役立つことを願っています

+0

あなたの答えをありがとうが、それはちょうどヌル値を返します... –

+0

あなたはsharepreference successfullで値を保存しないので、値を取得する前にチェックする必要があります。 –

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(); 

削除を取得し、アプリを再インストールします。

+0

このメソッドを試していただきありがとうございます! –

+0

String domain = String.valueOf(sipDomainView.getText());この値がnullかどうかを確認してください。 –

0

私のアプリはSharedPreferenceの代わりにTrayを使用したときに機能しました! あなたの答えの広告のためにあまりにも多くの男があなたの優しさ:)ありがとう!

関連する問題