这是我在onResume中的MainActivity呼叫:

@Override
public void onResume() {
    super.onResume();

    SharedPreferences settings = context.getSharedPreferences(INSTALL_PREFERENCE, 0);
    String referrerString = settings.getString(REFERRAL_KEY, null);
    Map<String, String> params = new HashMap<~>();
    if (referrerString != null){
         params.put("referrer", referrerString);
    }
}


这是扩展onResume的类中的我的BroadcastReceiver

@Override
    public void onReceive(Context context, Intent intent){
        try{
            String referrerString = intent.getStringExtra("referrer");

            if(null != referrerString){
                String referrer = URLEncoder.encode(referrerString, "UTF-8");
                context.getSharedPreferences(INSTALL_PREFERENCE, Context.MODE_PRIVATE).edit().putString(REFERRAL_KEY, referrer).commit();
            }

        }
        catch (Exception e){
            //don't handle exceptions for now
        }
    }


问题是,当我通过打开应用程序调用MainActivity时,我的应用程序在第一次打开时不会返回referrerSharedPreferences是否不会在打开的第一个应用程序上存储我的referrer的原因?我想在第一次打开而不是第二次返回referrer

最佳答案

这是您可以修复它的一种方法。使您的Activity实现OnSharedPreferenceChangeListener接口,当SharedPreference文件中注册的任何值更改时,该接口都会调用onSharedPreferenceChanged()回调。

如果时机正确,并且您在onResume()中得到一个值,则只需按照当前代码中的操作进行即可。

如果还没有值,请显示一个ProgressDialog,然后等待BroadcastReceiver填充该值。
一旦该值由BroadcastReceiver修改,请在onSharedPreferenceChanged()中捕获该值,然后根据需要使用它。

这是解决方案的一般结构:

public class MainActivity extends Activity
        implements SharedPreferences.OnSharedPreferenceChangeListener {

    SharedPreferences settings;
    ProgressDialog dialog;
    Map<String, String> params;
    String INSTALL_PREFERENCE = "installpref";
    String REFERRAL_KEY = "referral";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        dialog = new ProgressDialog(this);
        params = new HashMap<String, String>();

        settings = this.getSharedPreferences(INSTALL_PREFERENCE, 0);

        settings.registerOnSharedPreferenceChangeListener(this); //added
    }

    @Override
    public void onResume() {
        super.onResume();

        String referrerString = settings.getString(REFERRAL_KEY, null);
        if (referrerString != null){
            params.put("referrer", referrerString);
            //call whatever methods need params to have a value
            //.......
        }
        else{
            //Show progress dialog and wait for BroadcastReceiver to populate referrer
            dialog.setMessage("loading");
            dialog.show();
        }
    }


    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPrefs, String key) {

        if (key.equals(REFERRAL_KEY)){

            //dismiss dialog if it's showing
            if (dialog.isShowing()){
                dialog.dismiss();
            }

            String referrerString = sharedPrefs.getString(REFERRAL_KEY, null);
            if (referrerString != null){
                params.put("referrer", referrerString);
                //call whatever methods need params to have a value
                //.......
            }
        }
    }
}

关于android - 初始 Activity 的onResume上的Android Broadcast Receiver SharedPreferences为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30880593/

10-12 12:56